Ruckus
07/12/2019, 2:39 PMval thing = Thing().apply { setup(...) }
. While this is simple enough, I do it so often it would be nice to have an operator for it, that calls the function, but ignored the return value and returns the receiver instead, so I could do something like val thing = Thing()&.setup(...)
This may just be to my style of programming, but I figured I'd throw it out there to see if anyone else is interested.dalexander
07/12/2019, 2:55 PMkevinmost
07/12/2019, 3:06 PMmakeThing()?&.setup()
?Ruckus
07/12/2019, 3:38 PM&.
syntax, but I see no reason it couldn't just apply to anyhing. Basically it would just be an operator that calls a function and returns the receiver, so it could safe accessed or chained.Ruckus
07/12/2019, 3:40 PM^
or \
? (`Thing()^setup()`: makeThing()?^setup()
, `Thing()\setup()`: makeThing()?\setup()
)Ruckus
07/12/2019, 3:42 PMkioba
07/12/2019, 3:57 PMRuckus
07/12/2019, 4:04 PMapply
(albeit with an infix). You can already to .apply(Thing::setup)
, but it requires tying Thing::
, and only works if setup takes no arguments. I want to basically use it as a normal function call.kioba
07/12/2019, 4:27 PMcascade notation
with the ..
operator. I am not sure if this is useful but it basically reduces the apply to an operator.
The only problem with replacing functions with operators is after a while everyone wants their own operator. there are languages which supports creating your own but for a newcomer it is hard to understand without codebase specific knowledge.
I agree with you, sometimes it would be comfortable to have a cascade operator or add your own but I read somewhere that the Kotlin team decided to avoid such for better readability.karelpeeters
07/12/2019, 4:34 PMtmp
variable, but I don't think any of the proposed syntax in this thread is actually an improvement.kioba
07/12/2019, 4:36 PMRuckus
07/12/2019, 4:41 PMkarelpeeters
07/12/2019, 4:42 PMapply
is fine as-is.Hullaballoonatic
07/12/2019, 8:13 PMFoo() {{
//things
}}
Hullaballoonatic
07/12/2019, 8:16 PMclass Foo(var bar: Int = 0) {
constructor(op: Foo.() -> Unit) : this() {
op()
}
}
val foo = Foo {
bar++
println(this)
// etc
}
karelpeeters
07/12/2019, 8:22 PMarocnies
07/13/2019, 2:18 PM..
call? I don't mind Dart's syntax for it, but personally I prefer excluding it from the language in favor of apply