Grant Park
12/16/2020, 10:14 PMwhen
statement can evaluate its cases in constant time, because the compiler will change the switch structure to a map. Does anyone know if this is true?Marc Knaup
12/16/2020, 10:17 PMwhen
cases can be super complex.
Some cases can be optimized, like having only Int
and String
cases.Grant Park
12/16/2020, 10:19 PMGrant Park
12/16/2020, 10:19 PMMarc Knaup
12/16/2020, 10:20 PMwhen
?Grant Park
12/16/2020, 10:22 PMInt
cases for example. If the compiler replaced it with a map, the entire when statement can be evaluated instantly, whereas a typical switch structure would have to evaluate in linear time in the worst case.Grant Park
12/16/2020, 10:24 PMMarc Knaup
12/16/2020, 10:31 PMNir
12/16/2020, 10:32 PMNir
12/16/2020, 10:33 PMNir
12/16/2020, 10:33 PMNir
12/16/2020, 10:33 PMKirill Grouchnikov
12/16/2020, 10:44 PMGrant Park
12/16/2020, 10:55 PMwhen
that could easily grow in size to 10+ cases for a particular feature in an Android app: https://github.com/grant-park/ReduxEngineSampleProject/blob/master/app/src/main/ja[…]i/grant/reduxenginesampleproject/model/reducers/NotesReducer.ktKirill Grouchnikov
12/16/2020, 10:57 PMGrant Park
12/16/2020, 10:57 PMGrant Park
12/16/2020, 11:00 PMwhen
statements at a time on a single thread, so we are probably looking at 50 cases at most.
That is still a lot of iterations before rendering something on a screen.Grant Park
12/16/2020, 11:01 PMNir
12/16/2020, 11:02 PMNir
12/16/2020, 11:02 PMNir
12/16/2020, 11:03 PMNir
12/16/2020, 11:04 PMNir
12/16/2020, 11:04 PMGrant Park
12/16/2020, 11:04 PMGrant Park
12/16/2020, 11:05 PMGrant Park
12/16/2020, 11:05 PMNir
12/16/2020, 11:06 PMNir
12/16/2020, 11:06 PMNir
12/16/2020, 11:06 PMMarc Knaup
12/16/2020, 11:06 PMHashMap
in JVM is incredibly fast. Creating a HashMap
is not though.Grant Park
12/16/2020, 11:06 PMGrant Park
12/16/2020, 11:07 PMNir
12/16/2020, 11:07 PMNir
12/16/2020, 11:07 PMGrant Park
12/16/2020, 11:07 PMNir
12/16/2020, 11:08 PMNir
12/16/2020, 11:08 PMNir
12/16/2020, 11:08 PMNir
12/16/2020, 11:09 PMGrant Park
12/16/2020, 11:09 PMGrant Park
12/16/2020, 11:10 PMNir
12/16/2020, 11:11 PMGrant Park
12/16/2020, 11:11 PMNir
12/16/2020, 11:11 PMNir
12/16/2020, 11:12 PMGrant Park
12/16/2020, 11:12 PMGrant Park
12/16/2020, 11:12 PMwhen
with a thousand casesGrant Park
12/16/2020, 11:12 PMGrant Park
12/16/2020, 11:14 PMwhen
Grant Park
12/16/2020, 11:19 PMwhen
when walking into a codebase. I would flag that in a code review and ask why not use a simpler construct?Marc Knaup
12/16/2020, 11:31 PMwhen
cases. I guess due to type inference.
I had to split up my generated code into multiple functions.
https://raw.githubusercontent.com/fluidsonic/fluid-i18n/master/modules/data-regions/sources/common/RegionNames_normal.generated.ktMarc Knaup
12/16/2020, 11:33 PMGrant Park
12/16/2020, 11:37 PMMarc Knaup
12/16/2020, 11:37 PMMarc Knaup
12/16/2020, 11:37 PMMarc Knaup
12/16/2020, 11:37 PMGrant Park
12/16/2020, 11:38 PMGrant Park
12/16/2020, 11:38 PMNir
12/16/2020, 11:38 PMwhen
is, if its 4 cases, then yes, I'd agree the Map is weird. But Map is already a super standard technique for many situations over a when, like a polymorphic factory. The fact that it's extensible non-locally is a big advantage; in your example, you can have thousands of developers each registering their own cases into this Map, and affect the behavior of the "when" without touching a central (and perhaps incomprehensible) piece of codeMarc Knaup
12/16/2020, 11:40 PM.class
file. Then you can see how it was translated.
The Kotlin Bytecode Viewer in IDEA doesn’t show all optimizations.Marc Knaup
12/16/2020, 11:40 PMGrant Park
12/16/2020, 11:42 PMGrant Park
12/16/2020, 11:42 PMGrant Park
12/16/2020, 11:49 PMwhen
statements gets pretty heavy when looping through a bunch of them to produce new UI states in time to render on a screen.Grant Park
12/16/2020, 11:51 PMGrant Park
12/17/2020, 12:10 AMNir
12/17/2020, 12:17 AMraulraja
12/17/2020, 10:57 AMraulraja
12/17/2020, 10:57 AM