4 ms·
Well, it's obviously not your fault - I should have explained it better in the Language Reference (but it's a work-in-progress and I definitely will explain it
by stchacademy 6y ago
Well, it's obviously not your fault - I should have explained it better in the Language Reference (but it's a work-in-progress and I definitely will explain it thoroughly).
So...
The command "if?" takes two arguments: a boolean and a block.
Think of it like if? <something> [do something-else].
In this example, something is `even? x` and something-else is `print [x "is even"]`.
And yes, regarding the right-arrow symbol you are absolutely correct: it's syntactic sugar for wrapping a command in a block (provided that it's just ONE command - if there are more than one, you will have to use a block).
But it's still syntactic sugar as ALL symbols being used in Arturo. Pretty much like ".." is the symbol infix-alias of the command "range".
- Izkata 6y agoOh, I've used rebol 2 since like 1999 or 2000, so I got most of it, and the ".." was fairly guessable (also I've used it in bash), it's just the arrow that had me confused. Was also going to note the "?" in "if?" didn't make sense, due to how it's used in rebol, before finding core has both "if" and "if?", and "if?" does indeed return a boolean. Since both are there though, that does make it unnecessary in this particular example. However, there is one thing that could be done with "if": in arturo, it's documented to always return null, however in rebol it returns the value of the evaluated block, making "if/else" and "either" work as a ternary (and just "if" returns a none! if it was false).
- stchacademy 6y agoBasically, the idea behind "?" is "functions that return a boolean". But done in a consistent way. So, practically EVERY function that returns a boolean is - by convention - terminated with a "?". Now, why the two "if"s: Obviously, as you yourself mentioned, 'if? returns a boolean (the result of the condition) and 'if doesn't. So that's one use. The next use - which I will obviously have to make crystal clear in the Language Reference - is that if? (with the ?) is a must if you want to pair with an "else". Think of it this way. If?-else may appear as a language construct, but in fact they are two separate commands. The if? is an if - that pushes the result of the condition of the stack. And "else" is actually a reversed-if: it checks what is at the top of the stack (see: returned by the if? above) and if that is false - that is: the above if-condition was not true - it executes its block. So, in that case, you have to use an "if?" (and not a simple "if") The same thing happens with a case/when? construct (only, in that case, the condition blocks are actually concatenated),e .g. case [x] when? [<3] [print "it was less than 3"] when? [=3] [print "it was equal to 3"] else [ print "it was greater than 3"] :) :)
- stchacademy 6y agoRegarding if returning a value - and thus, being able to use it as part of an expression - is an absolutely interesting idea I've been thinking of implementing. However, I still don't know how. One idea would be to add an .attribute, e.g. if.get - and then have it return a value. Or, use a different function altogether. But then we'd have, 3-4 if's. That sounds like a bit of an overkill, I admit. I'm absolutely open to ideas :)