darkmoon_uk
03/04/2020, 6:36 AMUnit
. Any tricks to achieving this neatly?spand
03/04/2020, 6:39 AM{
...
nonUnitExpression()
Unit
}
Fleshgrinder
03/04/2020, 7:57 AM;
to do exactly this.
{ nonUnitExpression(); }
Another use case that I would like to cover is https://en.wikipedia.org/wiki/Method_cascading
obj.unitExpression()..nextFunOnObj()
elizarov
03/04/2020, 9:23 AMFleshgrinder
03/04/2020, 9:27 AMelizarov
03/04/2020, 9:31 AMphldavies
03/04/2020, 10:59 AM.let{}
to return Unit
but it's likely to confuse to the reader.Fleshgrinder
03/04/2020, 12:53 PMUnit
and I would like them to be as terse as the others when implementing interfaces. This is not possible…
fun f(): Unit = if (condition) doSomethingThatReturns() else doSomethingElseThatReturns()
In this case the only solution is to have:
fun f() {
if (condition) doSomethingThatReturns() else doSomethingElseThatReturns()
}
This can disturb the flow and make it much more verbose than it has to be.elizarov
03/04/2020, 1:20 PMfun f(): Unit = run { … }
But I, personally, as a matter of style, prefer to write fun f() { return … }
in this case. What would you suggest to make it less verbose?Fleshgrinder
03/04/2020, 1:31 PM;
. It’s semantics are clear imho and it yields the exact desired result. 🙂Fleshgrinder
03/04/2020, 1:33 PMfun f() = …;
instead of fun f() { … }
or fun f(): Unit = run {}
(which is the worst).Fleshgrinder
03/04/2020, 1:35 PMfun f() { … }
problematic is the fact that the default IntelliJ configuration is to insert an empty line before such a function and reformat the body. Hence, you cannot have …
fun a() = expr
fun b() { expr }
… because it will become …
fun a() = expr
fun b() {
expr
}
… unless you fiddle with the settings.Fleshgrinder
03/04/2020, 1:40 PM;
after each line unless you want to return in which case you can leave it off. The semantics in Rust are much more clearer than they would be in Kotlin. However, ;
is the (invisible) line terminator and using it as proposed is imho not too hard to understand. Of course I see that it might be confusing to some who are used to Java because = expr;
is the same as return expr;
for them. Maybe my Rust background is a bad influence here. 😄phldavies
03/04/2020, 1:47 PMinline operator fun Unit.rem(ignored: Any) = Unit
fun f() = Unit % if (condition) doSomethingThatReturns() else doSomethingElseThatReturns()
Can't say I like it thoughphldavies
03/04/2020, 1:47 PMoperator fun Unit.invoke(any: Any) = Unit
fun f() = Unit(if (condition) doSomethingThatReturns() else doSomethingElseThatReturns())
Fleshgrinder
03/04/2020, 1:50 PMrunBlocking
in Junit so that I can to @Test fun f() = runBlockingTest {
but it's too custom for anything else. I usually just live with the fact that it's not possible and tell my inner perfectionist that nobody else has these problems. 😄phldavies
03/04/2020, 1:52 PMfun f() { ... }
is preferable here, and that single line function bodies shouldn't have the same rule applied for prefixing with an empty newline.Fleshgrinder
03/04/2020, 1:53 PMphldavies
03/04/2020, 1:54 PM;
proposal, but with well known and understandable intent - I'm not sure how it's more verbosephldavies
03/04/2020, 1:54 PM=
-> {
, ;
-> }
Fleshgrinder
03/04/2020, 1:56 PM{ … }
usually indicates a lambda, hence, the default formatting IntelliJ applies makes sense. However, using ;
means end of stmt another follows and is already established (e.g. .map { expr; expr; expr; returnExpr }
. Hence, having fun f() { … }
will trigger the lambda though whereas fun f() = …;
triggers the end of stmt another follows thought. Well, at least for me. 😉Fleshgrinder
03/04/2020, 1:57 PMfun f() = { … }
vs fun f() { … }
😕phldavies
03/04/2020, 2:03 PM{ ... }
defines a block, or possibly a lambda depending on context. I would therefore read your example fun f() = ...;
to be no different than without the ;
, in that the function should return the value of the expression.Fleshgrinder
03/04/2020, 2:05 PMdarkmoon_uk
03/04/2020, 10:11 PMUnit
at the end of an expression lambda is the neatest solution in most cases. This does imply one is writing the lamdba for its side effects only, which is a code smell, but can be impractical to avoid when porting legacy code base from Java to Kotlin as I'm doing now.darkmoon_uk
03/04/2020, 10:14 PMAny
to .unit()
a result might be neatest? i.e. fun Any.unit() = Unit
Fleshgrinder
03/05/2020, 12:06 PMclass AutocompleteScope {
private val buf = StringBuilder()
infix fun <http://String.to|String.to>(value: Any) {
buf.apply {
append('\'')
append(this)
append("':'")
append(value)
append("',")
}
}
}
Would look much nicer without the extra {}
😎
class AutocompleteScope {
private val buf = StringBuilder()
infix fun <http://String.to|String.to>(value: Any) buf.apply {
append('\'')
append(this)
append("':'")
append(value)
append("',")
}
}
In this case I’m not using =
nor ;
since this would already solve my use cases.