1. short-circuiting
- Posted by "Frankie Dowling" <francis at gmx.co.uk> Jun 02, 2004
- 549 views
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? -- +64 (03) 3522885
2. Re: short-circuiting
- Posted by Derek Parnell <ddparnell at bigpond.com> Jun 02, 2004
- 531 views
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
3. Re: short-circuiting
- Posted by Don <eunexus at yahoo.com> Jun 02, 2004
- 562 views
> 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? Well, the documentation is quite clear: "Euphoria does short-circuit evaluation of if, elsif, and while conditions involving and and or" Cant do this in a return statement... But you can do this as a substitute function test(object whatever) if not sequence(whatever) then return 0 else return length(whatever) = 4 end if end function constant Val = test(0) Don Phillips National Insturments mailto: eunexus at yahoo.com