thhh
04/12/2021, 1:55 PM"creating and linking nodes" example {
I don't understand how there's a string "creating and linking nodes"
then after a example
Please explain!
fun main() {
"creating and linking nodes" example {
val node1 = Node(value = 1)
node1.next = node3 // shortened for brevity
println(node1)
}
}
diesieben07
04/12/2021, 1:56 PMexample
is probably an infix extension function on String
that takes a lambda as parameter:
infix fun String.example(body: () -> Unit) = TODO()
"foo" example { /* this is a lambda*/ }
mkrussel
04/12/2021, 1:56 PMthhh
04/12/2021, 2:01 PMTODO()
in this case. Can you please provide a code example for it so that I could run it and try out.
The code is this: https://pl.kotl.in/4_v-ixPxwdiesieben07
04/12/2021, 2:03 PMTODO
is a built-in kotlin function that just always throws an exception and has return type Nothing
so you can put it in place of a missing implementation and have your code still compile.thhh
04/12/2021, 2:05 PMinfix fun String.example(body: () -> Unit): Nothing = TODO()
fun main() {
"creating and linking nodes" example {
val node1 = Node(value = 1)
val node2 = Node(value = 2)
val node3 = Node(value = 3)
node1.next = node2
node2.next = node3
println(node1)
}
}
But got an error:
Exception in thread "main" kotlin.NotImplementedError: An operation is not implemented.
at experimentation.linkedlistyo.NodeKt.example(Node.kt:13)
at experimentation.linkedlistyo.NodeKt.main(Node.kt:16)
What am I missing here?diesieben07
04/12/2021, 2:06 PMTODO()
does, it throws NotImplementedError
if you actually call th efunctionthhh
04/12/2021, 2:10 PMprintln()
in the main function.
So as its not printing and just throwing error with TODO()
It's not as desired though.diesieben07
04/12/2021, 2:10 PMprintln
is inside a lambda, which you never call. As such the code within the lambda is never executed.mkrussel
04/12/2021, 2:12 PMinfix fun String.example(body: () -> Unit): Nothing = body()
thhh
04/12/2021, 2:13 PMinfix fun String.example(body: () -> Unit): Unit = body()
Thank you for guidance!Matteo Mirk
04/12/2021, 2:14 PMthhh
04/12/2021, 2:14 PMMatteo Mirk
04/12/2021, 2:14 PM