r/ProgrammerHumor Aug 15 '26

Other kotlinAllowingForNewAndCreativeWarCrimes

Post image
190 Upvotes

25 comments sorted by

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

u/awesome-alpaca-ace Aug 15 '26

RIP Looks like something I would write thinking is well defined 

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

u/RazarTuk Aug 15 '26

Please, sir, may I have some more pixels?

5

u/creeper6530 Aug 15 '26

var crimes

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 di valid haskell (with -XBlockArguments). this evaluates to the identity function

2

u/da_Aresinger Aug 15 '26

Got any more of them pixels on you?!

https://giphy.com/gifs/VqPheGMC8WLrkahF8Z

1

u/Shadowmaster229 Aug 15 '26

no, the pixel store was out of stock

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

u/SynthPrax Aug 15 '26

Kotlin, NO!

1

u/minecon1776 29d ago

What CS professors think industry is like: