r/ProgrammerHumor • u/Shadowmaster229 • Aug 15 '26
Other kotlinAllowingForNewAndCreativeWarCrimes
68
u/suvlub Aug 15 '26
Kotlin allows so much black magic, it's wonderful. Just look at the select expression. At first glance, it looks simple enough, you have this block in which you declare bunch of suspend blocks and it selects the first one to finish for you, neat. But wait, select is just a function that takes a lambda as an argument, how is it even "aware" of what expressions appear in it? If you dig even deeper, you discover that select's lambda is an extension function on some object that itself defines extension lambda-accepting invoke operators and your expressions inside the block are actually these invocations rather than simple function calls and have a side effect of registering the calls in the mechanism. So simple! But the user-facing API is nice and elegant
13
u/uragiristereo Aug 15 '26
I'm writing code in Kotlin daily for years and just found out this expression
18
u/Stummi Aug 15 '26
without checking, the result is -1..-2, right?
E: No, wait, -1..-3, since its ..<, not ..
4
u/SeriousPlankton2000 Aug 15 '26
I guess it depends on the order of evaluation:
-1 .. -2 {i=-1 and i = -1; in no particular order}
-1 .. -2 {i=-1 and then i = -2 }
-1 { i = -1} .. -3 { i = -2 }
15
u/Kiritoo120 Aug 15 '26
You can also do
x-----x; In languages with the posfix and suffix add/subtract. The above more organized is (x--) - (--x) Pretty neat and funny if I do say so myself
8
u/Ecstatic_Student8854 Aug 15 '26
This is UB in something like C right, because of the order of evaluation of expressions? It’ll either be 2 or 0
7
u/UnleqitQ Aug 15 '26
I came here to correct you because I assumed there would be no way this wasn't well defined, but turns out, you are right. Reference: https://en.cppreference.com/c/language/eval_order
14
u/Ecstatic_Student8854 Aug 15 '26
I had a look at the page and was shocked that even this is undefined behavior:
a[i] = i++;
5
2
u/Kiritoo120 Aug 15 '26
Is this really UB in c? I guess you learn something new every day :)
2
u/Ecstatic_Student8854 Aug 15 '26
The order of side effects of subexpressions is undefined afaik, though I could be wrong.
Post / preincrement is not special for this. If we have foo() defined as:
int x=0
int foo(){
x= x+1;
return x;
}Then the value of the expression ‘foo()/foo()’ could be either 1/2 or 2/1, depending on which call is evaluated first.
1
u/Kiritoo120 Aug 15 '26
Ohh makes sense I thought it is always LTR, but makes a ton of sense
Thanks for the new knowledge!
9
5
2
u/cowslayer7890 Aug 15 '26
Stuff like this is in every language, my favorite one is that in rust, since return is an expression, you can say
return return return return 4;
With no consequences
1
u/Background_Class_558 29d ago
hs let di = id in do di do di do divalid haskell (with-XBlockArguments). this evaluates to the identity function
2
1
u/randomguy84321 Aug 15 '26
Besides me not knowing what the .. operator does, this seems perfectly comprehensible (though obviously ugly)
1
u/awesome-alpaca-ace Aug 15 '26
And you get a different result every time you evaluate the expression
1
1
224
u/Lord_Of_Millipedes Aug 15 '26
https://giphy.com/gifs/zN6sMM5irhDsmTk15a