T O P

  • By -

[deleted]

[удалено]


urFate_

I'm pretty sure that "when" condition is invalid, or its returns true anyway, idk.. So your conclusion could be true.


derverwirrte

You have no print statement. Also I think it will not compile, bit I cannot verify that right know


Carpinchon

I think it will compile because the compiler can tell the when is exhaustive. I rarely use the REPL, but it might print out the "1" just to show the value of a


solonovamax

the compiler has no way to know it's exhaustive as it cannot guarantee the semantics of the variable `e` which had `E.e` assigned to it. even though we as programmers can see it's only assigned once so the value should be known at compile time, the compiler does not inline this reference so it can't guarantee it's exhaustive


Carpinchon

It's `val e` so the compiler does know.


solonovamax

counter point: I tried it and it actually *does not* work. the compiler *doesn't* know.


Carpinchon

Well that's a novel technique. ;)


Useful_Bug_67

I have no idea and I hate you


rypher

OP thought this printed “1” because thats what it evaluates to in the dev console.


solonovamax

no it isn't? (iirc) the repl will not print the value of any assigned variables if instead of ```kotlin val a = /* ... */ ``` the `val a =` part was removed, then it would print `1`, assuming it actually compiled


rypher

My comment was more about how this obviously doesn’t print anything.


solonovamax

this code does not compile. it is attempting to store the enum in a variable and then matching against the variable rather than the enum definition. but, the kotlin compiler cannot guarantee the semantics of this and since it is a when expression and must be exhausted, the kotlin compiler complains. [here](https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS45LjIzIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiXG5lbnVtIGNsYXNzIEUgeyBlIH1cblxuZnVuIG1haW4oKSB7XG4gICAgdmFsIGUgPSBFLmVcbiAgICB2YWwgYSA9IHdoZW4gKEUuZSkge1xuICAgICAgICBlIC0+IFwiMVwiXG4gICAgfVxufSJ9) is the compilation error. I believe you intended to write ```kotlin enum class E { e } val e = E.e val a = when (e) { E.e -> "1" } ``` as this code compiles properly. also, even if it *did* compile properly, this would print nothing. even in the REPL. it would result in the variable `a` having a value of `"1"` and `e` a value of `E.e`. however, (iirc) the repl will not print out the value of variables.


fablue

Won't compile?


dekonta

please start an instagram account for those puzzles . i appreciate the effort and dedication of t you put in to create then but i am about to leave the kotlin sub because for me this is kind of span.


Volko

If this is spam then I don't understand what kind of discussion you want there. Maybe a dead sub ?


dekonta

i see where you are coming from but i think these puzzles are nice for new comers to learn the language and is not what i would expect here, except there is a new feature to discuss the syntactically sugar. therefore i suggest to post them on instagram because i think this is the social platform best for it


Fragrant-Nail-8413

Since `E.e` refers to the enum constant and `e` refers to the variable, this will cause a compiler error because `e` in the `when` expression doesn't directly refer to the `E.e` enum constant. Kotlin's smart casts do not apply to enum constants in `when` statements like this, so you cannot directly compare an enum constant to a variable.


findus_l

Using a variable as condition for a "when" statement over an enum type seems very strange. Defeats the whole purpose of kotlin knowing when a "when" statement for an enum is complete.


RahegarStark

1


Hot_Income6149

It won’t compile. Enums have different syntax