Kev
02/16/2024, 2:42 AM.map(::MyDto)
or .map(::println)
?Joffrey
02/16/2024, 3:03 AM::println
is a function reference. map
is a higher order function, meaning that it takes a function as a parameter. A value of type function can be in different forms. You can pass a lambda expression like { println(it) }
, or you can pass a variable of a function type, or you can pass a reference to a function with this ::funName
syntax.Joffrey
02/16/2024, 3:04 AMprintln
or the MyDto
constructor and passing the result to map
here, you're passing the function itself. This is what higher-order functions are all about.Kev
02/16/2024, 3:53 AMKev
02/16/2024, 4:30 AMJoffrey
02/16/2024, 9:28 AMmap
. The ::
syntax denotes a function reference. Now whether that is idiomatic is debatable. Lambdas are extremely common, and make no perf difference when the higher-order function is inline
. Function references are less common I would say, because you can't use them if the signature of the function doesn't match with what the higher order function expects. I wouldn't say one is more idiomatic than the other, though.Joffrey
02/16/2024, 9:29 AMAlso, how would you construct aligning functions that conform to higher order functions? Could you please provide a simple example?
Sorry I don't get the question. Could you please elaborate?
Joffrey
02/16/2024, 9:30 AM