Daniele Segato
11/21/2019, 7:06 AMval x = SomeInterface { }
Thanks to SAM.
However when i try the same thing on kotlin interface I'm forced to create an anonymous class:
val x = object : SomeInterface {
fun someMethod() { }
}
If i instead create a type alias for it I can write it without the object ceremony but I've no way of specifying what it is:
typealias SomeInterface = (): Unit
// ...
val x = { }
Yes i know i can do
val x: SomeInterface = { }
But picture this situation:
val x: SomeInterface = when (foo) {
"A" -> { { } }
"B" -> { { } }
}
I need double {
, which is horrible.
I think it would be much better to be able to write;
val x = when (foo) {
"A" -> SomeInterface { }
"B" -> SomeInterface { }
}
Is there a way to do this or a way to improve the previous example?bezrukov
11/21/2019, 7:54 AMSomeInterface(lambda): SomeInterface = lambda
Then you will be able to do what you wantDaniele Segato
11/21/2019, 8:05 AMtypealias SomeInterface = (param: SomeParam) -> SomeResult
@Suppress("NOTHING_TO_INLINE", "FunctionName")
inline fun SomeInterface(noinline lambda: SomeInterface): SomeInterface = mapper
and now I can write a more readable code :)Hullaballoonatic
11/21/2019, 6:14 PMDaniele Segato
11/22/2019, 12:21 PMinterface SomeInterface {
operator fun invoke(param: SomeParam): SomeResult
}
?
or do you mean like this?
interface SomeInterface {
fun doStuff(param: SomeParam): SomeResult
companion object {
operator fun invoke(lambda: SomeInterface): SomeInterface = lambda
}
}
?
If you meant any of those, I fail to see how they'd help in my situation / how to make use of them.
Can you give an example of what you meant and how to use it?
Thanks