Tim Kuzmin
07/21/2019, 1:49 PMfun <T> Boolean.ternary(first: T, second: T) =
if (this) first
else second
Any pro-programmer will tell u that in cases like this
(stringA != null).ternary(stringA!!, stringB!!)
we will have npe(parameters of function calculates before call).
Lets make our function inline and add reified type:
inline fun <reified T> Boolean.ternary(first: T, second: T) =
if (this) first
else second
Nice! Now for non-primitives classes we will not have any trouble.
I guess u’ve already understand what I mean. For primitives it doesnt work, cause even for inline function kotlin calculates primitive expressions and then call function. For me it’s looks like a bug. Does anybody know will it be fixed for next versions?Dominaezzz
07/21/2019, 1:50 PMDominaezzz
07/21/2019, 1:51 PMrequireNonNull
for inspiration.Dominaezzz
07/21/2019, 1:52 PMfirst
and second
are lambdas.Tim Kuzmin
07/21/2019, 1:55 PMTim Kuzmin
07/21/2019, 3:09 PMkarelpeeters
07/21/2019, 7:18 PMinline
nor reified
have any effect on when the parameters are evaluated, so I don't understand what you're talking about. It doesn't matter whether T is primitive either.karelpeeters
07/21/2019, 7:18 PM