Re: short-circuiting

new topic     » goto parent     » topic index » view thread      » older message » newer message

FDe(censored) wrote:
> 
> I haven't used euphoria for a while, and have just got back into it. Quick
> question
> about short-circuiting a function:
> from memory euphoria allowed this to short-circuit...? but does not work
> however
> 
> 
> function test(object whatever)
> 
> 	return (sequence(whatever) and (length(whatever) = 4))
> 	
> end function
> 
> test(0) errors because it goes on to call length(), however this code:
> 
> 
> int test(int whatever) {
> 	if ((++whatever == 1) && (++whatever == 2)) {
> 	}
> 	return whatever;
> }
> 
> prints 1 as expected.
> 
> so how do I achieve the same logic in euphoria?

Ok, Euphoria does *not* do short circuit evaluation on 'return' statements.
Only 'if', 'elsif', and 'while' support this.

The above routines could be written as ...

 function test(object whatever)
 
    if (sequence(whatever) and (length(whatever) = 4)) then
       return 1
    else
       return 0
    end if
 	
 end function


and the other ...

 function test(integer whatever)
      if whatever = 0 then
          return 2
      else
          return whatever + 1
      end if
      -- Which is SO MUCH easier to understand than...
 	--if ((++whatever == 1) && (++whatever == 2)) {
 	--}
 	--return whatever;
 end function

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu