kierans777
06/09/2021, 6:27 AMfun add(a: Int, b: Int): Int = a + b
val multiply: (Int, Int) -> Int = { a: Int, b: Int -> a * b }
val add3 = add.curried()(3) // does not compile
val multiply2 = multiply.curried()(2)
Where this affects me is that I want to have a function that can take different parameter types eg: an Int or a String. Standard function overloading gets me out of this
fun add(a: Int, b: Int): Int = a + b
fun add(a: String, b: String): Int = add(Integer.parseInt(a), Integer.parseInt(b))
However I lose the nice FP'ness ie: currying, composability, etc.Mitchell Skaggs
06/09/2021, 6:31 AMmultiply
function by just stating multiply
, you have to use the function reference syntax ::multiply
. Does that work for you?Mitchell Skaggs
06/09/2021, 6:32 AMkierans777
06/09/2021, 6:36 AM::add
worked. So simple. Thanks @Mitchell Skaggskierans777
06/09/2021, 6:38 AMraulraja
06/09/2021, 12:20 PMfun <A> foo(a: A) = {}
val f: (Int) -> Unit = ::foo<Int>
//Type inference failed: Not enough information to infer parameter A in fun <A> foo(): () -> Unit
//Please specify it explicitly. Type arguments are not allowed.
val f2: (Int) -> Unit = { n: Int -> foo(n) } //ok