Log in

View Full Version : short circuit evaluation in Simba



Er1k
01-14-2012, 02:16 AM
Last time I'm checking, the pascal that simba uses does not have the keywords and_then or_else that ensures short circuit evaluation is done from left to right. My question is, is short circuit evaluation supported? Or must we use nested Ifs to ensure the results are evaluated in the order we specify?

Brandon
01-14-2012, 02:23 AM
Is this what you mean? If it is, then why not just switch the Left = Right ---to--- Right = Left?

Function EvalLR: Boolean;
var
X, Y, A, B: Integer;
J, K: Boolean;
begin
X:= 1; Y:= 1; A:= 2; B:= 3;

L:= (X = Y);
R:= (A = B);

//What you want:
//if ((X = Y)) = ((A = B))
//In other words..
//if (L = R)..

//The way to do this in pascal I guess:

if (A = B) then
if(X = Y) then
Result:= True;

// OR:
if (L = R) //or (R = L)

end;

noidea
01-14-2012, 02:30 AM
//Short Circuit eval

var
a, b, c, d: integer;
bA, bB: boolean;
begin
a := 0; b := 1; c := 3; d := 3;
bA := (a = b);
bB := (c = d);
// statement should be false after testing bA, and never test bB
// iirc thats what short circuit evals about
// I can't remember if its left to right, or right to left
if (bA and bB) then
writeln('netanyahoo');
end;

Er1k
01-14-2012, 02:50 AM
Ah that's exactly what I'm looking for. Nevertheless it sucks for me that the language does not have it implemented by default :( (Sorry I was a lazy java guy)

Sabzi
01-14-2012, 11:14 AM
Doesn't Lape supports it?
Let me check.
It is, if you switch it on, or I don't know what it does by default, but you can definitely switch it on.

It's on by default, you can switch it off.

program ShortCircuitEvaluationTest;
//{$B OFF}

function Bool(i: Integer): Boolean;

begin
WriteLn(i);
Result := i;
end;

begin
if(Bool(0) and Bool(1)) then
WriteLn('wat');
end.

With lape of course. Neat.

p.s.: I love Lape.

Er1k
01-14-2012, 01:09 PM
Doesn't Lape supports it?
Let me check.
It is, if you switch it on, or I don't know what it does by default, but you can definitely switch it on.

It's on by default, you can switch it off.

program ShortCircuitEvaluationTest;
//{$B OFF}

function Bool(i: Integer): Boolean;

begin
WriteLn(i);
Result := i;
end;

begin
if(Bool(0) and Bool(1)) then
WriteLn('wat');
end.

With lape of course. Neat.

p.s.: I love Lape.

Thanks loads I don't realize there's a LAPE interpreter option. :D