11/25/2011

Booleans and boolean expressions

It is highly likely that you have noticed that boolean expressions are horribly supported in TBS alpha-2.9 and older. Luckily, alpha-3 may come to the rescue!
The following(pointless but interesting) program and its explanation, should provide you with an idea of how alpha-3 will handle boolean expressions.
using(Stdio);
#func() {
    ?blnTest =: 1 == 1 && 1 <> 2 && :<>false;
    _if(:false || 1>0 && :?blnTest) {
        !out(~"New, working expressions! Finally!\n");
    }
    : : :true;
};
!func();
_die();
The first thing you should know is that the boolean operator was changed from '|' to ':', I know, neither are "logical", but it's really hard to find a more logical character that a normal keyboard has.

Besides that, you must also know that the previous "=/" operator has been changed to "<>", the "=<" operator to "<=", the "=>" operator to ">=" and the previous "/" to "<>". The "&" operator is now "&&", just like the "|" was changed to "||". Notice however that "|" still exists, as the exclusive or(XOR).
I truly believe this is a good change as this notation is more widely used.

The return statement(": : :true;") might seem a little weird, but it is only logical: the first ':' represents the return command, the second one indicates that we will be returning a boolean expression, and the last one that we read a boolean value in that expression. For example, if we wanted to return a string we would write ":~?string;", or if we wanted to return a boolean as the result of a string comparison we would write  ": : ~"string" == ?string;".

No comments:

Post a Comment