Viktor Qvarfordt
05/16/2019, 8:48 PMmyList.map(muFun)
? I know there is myList.map { myFun(it) }
but it requires me to explicitly call the function, if we compare to other languages this is often an anti-pattern.Ruckus
05/16/2019, 8:50 PMmyList.map(::myFun)
Ruckus
05/16/2019, 8:50 PMViktor Qvarfordt
05/16/2019, 8:52 PM::
is new to me. Thanks!Viktor Qvarfordt
05/16/2019, 8:52 PMelizarov
05/16/2019, 9:01 PMstreetsofboston
05/16/2019, 9:02 PMmyObject::myFunction
.
If it were myObject.myFunction
it would be the value of a property… not a method-reference….Viktor Qvarfordt
05/16/2019, 9:03 PMf
in myList.map(f)
cannot simply refer to the function f
, like in many other languages (js and python come to mind)streetsofboston
05/16/2019, 9:03 PMMyClass::myFunction
(where MyClass.myFunction
would be the value of a class-property)streetsofboston
05/16/2019, 9:04 PM::myTopFunction
, because myTopFunction
would be the value of a top-propertyViktor Qvarfordt
05/16/2019, 9:04 PMViktor Qvarfordt
05/16/2019, 9:05 PMf
and ::f
could refer to different things?streetsofboston
05/16/2019, 9:06 PMf
would get you the value of a top property named ‘f’, while ::f
would be the method-reference to a top-function with the name ‘f’.Viktor Qvarfordt
05/16/2019, 9:08 PMstreetsofboston
05/16/2019, 9:08 PMval f = ""
fun f() = ""
elizarov
05/16/2019, 9:09 PMf
, but this convention does not seem to gaining main-stream popularity. However, you are always free to write your functions in "modern JS style" as val f = { x: Type -> ... }
and then refer to them with a simple f
.Ruckus
05/16/2019, 9:09 PMval f: (Int) -> Int = ...
fun f(i: Int): Int { ... }
fun test(func: (Int) -> Int) { ... }
test(f)
test(::f)
Both calls to test
are valid. The first passes the val f
, the second passes the fun f
Ruckus
05/16/2019, 9:09 PMViktor Qvarfordt
05/16/2019, 9:10 PMval f = { x: Type -> ... }
is allowed… 😉streetsofboston
05/16/2019, 9:11 PMi: Int
from the fun f
)
val f: (Int) -> Int = ...
fun f(): Int { ... }
fun test(func: (Int) -> Int) { ... }
test(f)
test(::f) // compiler error
Viktor Qvarfordt
05/16/2019, 9:12 PMval f = ""
fun f() = ""
fun f(x: Int) = ""
does ::f
refer to the version with zero or one argument?elizarov
05/16/2019, 9:16 PMelizarov
05/16/2019, 9:16 PM