[Grace-core] Short-circuiting elseif

Michael Homer mwh at ecs.vuw.ac.nz
Thu Mar 15 15:46:39 PDT 2012


Hi,
I've been converting the minigrace control structures to methods
(previously special-cased in the parser). I have switched while-do and
for-do over without any issues, leaving if to last because of the
variadic nature of elseifs. Now that I've come to it I have a
conceptual question about how it works as a method call. Given:

  def data = [1,2]
  if (sum(data) < 5) then {
    ...
  } elseif (product(data) > 20) {
    ...
  }

that should be a request for:

  method if(test1 : Boolean) then(block1 : Block) elseif(test2 :
Boolean) then(block2 : Block) {
    ...
  }

test1, block1, test2, and block2 are method arguments that must be
evaluated when the request is made. That means in the example that
sum(data) and product(data) are evaluated, along with whatever side
effects they have. I have had the special case give ordinary if-elseif
semantics, where evaluation stops as soon as a condition is met.

elseif is not actually mentioned in the specification at the moment
(r297). Does it exist, or do you always write "if (...) then {} else {
if (...) {} }"? Most straightforwardly elseif takes a block, which
means "if (test1) then {} elseif {test2} then {}", which is a little
ugly. Alternatively all tests could be in blocks for consistency
across a call, even though the first one is always guaranteed to
execute.
-Michael


More information about the Grace-core mailing list