abbic
02/04/2022, 11:48 AMClass {
fun foo() { // normal method definition }
val foo = { // same method implementation, but its a lambda }
}
therealbluepandabear
02/04/2022, 11:58 AMtherealbluepandabear
02/04/2022, 12:00 PMabbic
02/04/2022, 12:03 PM::
! but yeah, i guess apart from readability i wondered if there were important cases where you might find a method was necessary over a val.
i suppose that, if fancy took you, you might never need to define functions with fun
at all! though it probably incurs some small overhead if i had to guesstherealbluepandabear
02/04/2022, 12:06 PMtherealbluepandabear
02/04/2022, 12:07 PMgildor
02/04/2022, 12:13 PMtherealbluepandabear
02/04/2022, 12:13 PMclass FooClass() {
fun fooFunc() {
println("hi")
}
val fooLambda = {
println("hi")
}
}
import kotlin.Metadata;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
public final class FooClass {
@NotNull
private final Function0 fooLambda;
public final void fooFunc() {
String var1 = "hi";
boolean var2 = false;
System.out.println(var1);
}
@NotNull
public final Function0 getFooLambda() {
return this.fooLambda;
}
public FooClass() {
this.fooLambda = (Function0)null.INSTANCE;
}
}
Roukanken
02/04/2022, 12:16 PMnull.INSTANCE
?
I mean I haven't met much with bytecode stuff, but that sounds iffytherealbluepandabear
02/04/2022, 12:19 PMJohann Pardanaud
02/04/2022, 1:19 PMRoukanken
02/04/2022, 1:31 PMdata class Test(val value: Int) {
fun plus1(other: Test) = Test(value + other.value)
val plus2 = { other: Test -> Test(value + other.value) }
}
fun main() {
val tests = (1..5).map { Test(it) }
println(tests.reduce(Test::plus1))
println(tests.reduce { a, b -> a.plus1(b) })
// println(tests.reduce(Test::plus2))
println(tests.reduce { a, b -> a.plus2(b) })
}
the commented line does not work, because (Test::plus1)(a, b)
== (Test::plus2)(a)(b)
therealbluepandabear
02/04/2022, 2:19 PM