Issue with the if else usage

Hi

I am trying to write multiple if, else if, else clauses

The javascript output is not consistent.

I wanted to use switch but in this particular case if, else if, else was more appropriate.

Please help.

1 Like

What isn’t consistent? It looks fine to me. You’re returning 0 from the two last branches, maybe that’s what’s confusing you?

Hi @zth

The else if is completely missing in the javascript output. I should have been more specific in my explanation

It’s optimized away because it’s dead code - the final else branch covers it as well, because they return the same thing. Try making the else if branch return 1 instead and you’ll see it appears.

Side note: A good example of how ReScript usually emits code that’s more efficient than the hand written equivalent :smile:

5 Likes

Aha rescript is too smart for me :stuck_out_tongue:

2 Likes

Hey Gabriel,
Do you know how far this goes? is it optimizing away simple tails, or can it go deeper?

Waiting to hear from @cristianoc about his exploration of AI solutions, but have been wondering about duplicate code warnings, etc. possible in the rescript compiler.

Thanks
Alex

[edit: I would love to know what the performance benefits from these optimizations. Surely other layers of the compilation stack are doing the same?]

1 Like

I’m honestly not familiar enough with that part of the code base. But, I’d love it if we somehow gathered all significant optimiziations as examples somewhere. That would be awesome to have. If anyone is up for doing that I’d be happy to assist.

3 Likes

Not sure but optimizing conditionals when the branches return the same value might be the thing needed to optimize complex pattern match cases.
And would fire in general on any conditional.

2 Likes

Hi @zth

As a addon question to this, could the rescript compiler warn about it. So that we could see it on the rescript side already, instead of check the generated js.

Lately, the only time I am checking the generated js is when I am writing new bindings. Otherwise I have completely relied on the rescript compiler and tests.