hi, can i defined a function which can be called l...
# announcements
r
hi, can i defined a function which can be called like "diff{//do some}{// do some}" ?
k
either
diff({//do some},{// do some})
or
diff({//do some}){// do some}
r
thx, i choose diff{}(){}
k
That's not one of the options I gave, but if you really want that you can curry your function and do that, but I'd advise against it as it would come with some overhead.
1
b
The first syntax
foo {}{}
is actually possible, although it comes with some performance overhead and I would advice against it. The trick is that foo returns a function taking another lambda as it’s argument. https://pl.kotl.in/NFoCKXcuP
k
That does not compile.
With currying (what you did) you can get
(foo{}){}
and
foo{}(){}
to work but not
foo{}{}
b
wow after reloading the site it no longer does. I swear I just had it working.
I guess I mussed have imagined pressing run after changing
(foo{}){}
to
foo{}{}
. I would say too bad, but than I didn’t think it good style anyways.
e
where question mark will have to be replaced with your types
k
Are you sure, I can't seem to reproduce that. I'm pretty sure the max 1 lambda outside parentheses is an parser limitation.
e
I thought we could🧐
oh I couldn't either, sorry!
j
you could use infix functions, e.g.
Copy code
infix fun <T> (() -> T).diff(other: () -> T) {}
    
    fun doIt() {
        { "this" } diff { "that" }
    }