-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chained conditionals: how they work, how they *should* work and how we can optimize them #1247
Comments
The case of
|
As a useful and important sidenote to the above explanation: Arturo already has a one-statement, Now, why have this too? Do we just need another function simply to do the exact same thing? It's not that simple. What it comes down to again is the fact that we would be talking about one single statement (instead of two). And that means that, using x: 0
y: switch x=0 -> 1 -> 2 ; if x is 0, set y to 1, otherwise set it to 2 or using our x: 0
y: (x=0)? -> 1 -> 2 ...which looks good and tidy; plus, if we were to use y: 0
x: 0
if? x = 0 -> y: 1
else -> y: 2 (Verbose... too verbose! lol) |
Note no 2: b: function [x]-> inspect to :bytecode x
identical?: function [z][
one? unique map @z 'a -> to :bytecode a
]
ib [
if? x=2 -> print x
else -> print x*2
]
print identical? [
[
if? x=2 -> print x
else -> print x*2
]
[
if? x=2 -> print x
else -> print x*2
]
[
if? x=2
-> print x
else
-> print x*2
]
[
if?
x=2
-> print x
else
-> print x*2
]
[
if? x=2 -> print x
; some comment
; another comment
else -> print x*2
]
[
switch x=2 -> print x
-> print x*2
]
[
(x=2)? -> print x
-> print x*2
]
] Result: [ :bytecode
================================
DATA
================================
0: x :word
================================
CODE
================================
consti2
load0
jmpifne @4
load0
print
goto @4
consti2
load0
mul
print
end
]
true (Not only do newlines not affect the produced bytecode, but... apparently |
At first I found a bit confusing the fact the documentation doesn't mention the relation between But at the end everything became clear.
|
The truth is the documentation could be lacking in various cases. And that is one of them. Btw, you're more than welcome to make a PR! 😉 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue is not properly speaking a bug, but its most definite goal is to deal with - perhaps by design - features which often lead to issues: namely chained conditionals, or more concretely
if?
/else
structures &case
/when?
/.../else
structures.Also, we've thoroughly discussed the issue with @RickBarretto, but I think it'd be better to have all of this in the open, not only as a means of exchanging ideas publicly, but also so that we can gather all the necessary information/tests/etc in one place. 🚀
The text was updated successfully, but these errors were encountered: