suppose I have a function like ``` fun asString()...
# codingconventions
t
suppose I have a function like
Copy code
fun asString(): (Any) -> String = Any::toString
I can call it in 2 ways -
asString()(anObject)
-
asString().invoke(anObject)
I believe the first way would be more idiomatic, but coming from java I still prefer the second (
()()
looks too weird to me). For the sake of who will come after me, which version would you suggesting to use?
m
You could just do
val asString = Any::toString
. Then you can call it with
asString(anObject)
.
👍 4
t
true as well (which is actually what I have in the real world scenario)
although the original question still stands (I am also composing function, in that case I have to use `fun`s)
Copy code
fun <A, B, C> ((A) -> B).then(then: (B) -> C): (A) -> C = { input -> then(this(input)) }
at call site is either: -
a.then(b).invoke(input)
-
a.then(b)(input)
a
and
b
are something like
val a = Any::toString
m
You can still compose functions even though you use
val
. But anyway, I think
a.then(b)(input)
looks better.
👍 1
b
I find using the operator syntax has looked better in almost every scenario. I sometimes will use
invoke(...)
over
this(...)
. There are other times where I've been restricted by Kotlin semantics (ie,
b[c]?.doSomethingExisting() ?: b.set(c, default())
since
... ?: (b[c] = default())
is not allowed)