LastExceed
05/13/2021, 8:29 AM?:
similar to how there's get()
for []
? if not, why not ?Big Chungus
05/13/2021, 8:35 AMCyril Find
05/13/2021, 8:43 AMgetOrDefault
LastExceed
05/13/2021, 8:44 AMLastExceed
05/13/2021, 8:45 AMBig Chungus
05/13/2021, 8:45 AMgetOrDefault("key", "default value")
is the same as get("key") ?: "defaultValue"
given Map<String, String>
LastExceed
05/13/2021, 8:48 AMephemient
05/13/2021, 8:51 AM&&
||
either. those and ?:
do not evaluate the right hand side unless needed; that is fundamentally different than a function, where all arguments are evaluated before the call.LastExceed
05/13/2021, 8:53 AMLastExceed
05/13/2021, 9:03 AMBig Chungus
05/13/2021, 9:05 AMLastExceed
05/13/2021, 9:06 AMBig Chungus
05/13/2021, 9:06 AMinline fun <T> T.elvis(fallback: () -> T) = this ?: fallback()
val x = null
val y = x ?: 42
val z = x.elvis { 42 }
Big Chungus
05/13/2021, 9:07 AMinline
keywoard. Read on inline functions in kotlin docs 😉 Your case is one of the main use-cases for inline functionsLastExceed
05/13/2021, 9:10 AMinline
, i just dont see the point of using it here, nor how it makes the result any less verbose. technically you even made it more verbose by adding 1 more wordLastExceed
05/13/2021, 9:12 AMinline
to my first implementation rather than my second, but i just tried that and it still evaluates the parameter in advanceBig Chungus
05/13/2021, 9:12 AMBig Chungus
05/13/2021, 9:12 AMLastExceed
05/13/2021, 9:14 AMinline
also unwraps the lambda parameter. but how does this make api nicer ? isnt this just a performance optimiuation ?Big Chungus
05/13/2021, 9:14 AM?:
LastExceed
05/13/2021, 9:16 AMLastExceed
05/13/2021, 9:16 AMBig Chungus
05/13/2021, 9:16 AMBig Chungus
05/13/2021, 9:16 AMBig Chungus
05/13/2021, 9:17 AMLastExceed
05/13/2021, 9:19 AMgetText().elvis{"no text"}.toUpper()
is nicer than (getText() ?: "no text").toUpper()
right ?Big Chungus
05/13/2021, 9:21 AM?:
Big Chungus
05/13/2021, 9:21 AM.elvis()
function, at least make it inlineLastExceed
05/13/2021, 9:24 AM?:
is clear enough, but i think it becomes a bit messy in nested scenarios (which to be fair is quite the edge case)
1 more question: why doesnt inlining the first implementation do the trick? inlining basically means pasting the content of the function at the call site which would leave us with just ?:
which shouldn't evaluate the fallback unless needed, no ?Big Chungus
05/13/2021, 9:25 AMBig Chungus
05/13/2021, 9:25 AM.elvis()
tooLastExceed
05/13/2021, 9:26 AMBig Chungus
05/13/2021, 9:26 AMLastExceed
05/13/2021, 9:26 AMBig Chungus
05/13/2021, 9:26 AMLastExceed
05/13/2021, 9:27 AMBig Chungus
05/13/2021, 9:31 AMrun{}
is not getting evaluatedLastExceed
05/13/2021, 9:32 AMLastExceed
05/13/2021, 9:34 AMBig Chungus
05/13/2021, 9:36 AMBig Chungus
05/13/2021, 9:36 AMLastExceed
05/13/2021, 9:38 AMBig Chungus
05/13/2021, 9:40 AMelvis
LastExceed
05/13/2021, 9:41 AMLastExceed
05/13/2021, 10:02 AMT : Any
is needed to guarantee that the lambda doesn't return null
inline
makes sure we don't loose any performance as it unwraps the lambda at compile timeBig Chungus
05/13/2021, 10:04 AMLastExceed
05/13/2021, 10:05 AMLastExceed
05/13/2021, 10:17 AM?:
allows using return
as fallback, so elvis
should too. therefore no crossinline
neededBig Chungus
05/13/2021, 10:18 AMLastExceed
05/13/2021, 10:19 AMinline
already do that ?LastExceed
05/13/2021, 10:19 AMinline
in the first placeBig Chungus
05/13/2021, 10:20 AMLastExceed
05/13/2021, 10:21 AMLastExceed
05/13/2021, 10:22 AMcrossinline
just prevents non-local returnsBig Chungus
05/13/2021, 10:29 AMLastExceed
05/13/2021, 10:29 AM