Re: Short-cut evaluation Question

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

Travis wrote:

> I've got a question here about short-cut evaluation.  As reference here, I'm
> using the Linux version of the 2.4 interpreter.  I'm pretty sure this is a
> bug between my ears as opposed to a bug in the interpreter, but I'd like to
> get some clarification on this.
>
> I have the following type declaration:
>
>
> global type Widget(object o)
>
> 	if not atom(o) then
> 		return false
> 	else
> 		return (o = 0 or TypesMatch(o, "QWidget") or IsBaseClass(o, "QWidget"))
> 	end if
>
> end type
>
>
> Now then, in this particular instance I have confirmed via trace that o is
> equal to zero.  It seems to me that since we are testing using "or" in the
> return statement, if any expression in the test evaluates to "true," then the
> entire expression would be true.  If that is the case, then short-cut
> evaluation should stop processing the expression at that point and return
> true.
>
> Thus, in the case of the return statement here, the interpreter should see
> that, indeed, o = 0, and then return true.  But it is not ... it in instead
> evaluating TypesMatch(o, "QWidget"), which is causing my library to become
> quite upset, since o is a NULL pointer.
>
> Now, it would be easy for me to add the line
>
> elsif o = 0 then
> 	return true
>
> But apparently I'm not understanding something about short-cut evaluation
> here.  Could anyone enlighten me?
>
> Thanks,

Current Euphoria versions only use short-circuit evaluation in
conditions tested by "if", "elsif" or "while". I.e. if you want
short-circuit evaluation, write the code above like this:

global type Widget(object o)
   if not atom(o) then
      return false
   elsif (o = 0 or TypesMatch(o, "QWidget") or IsBaseClass(o, "QWidget")) then
      return true
   else
      return false
   end if
end type

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  Money is the root of all evil.
 \ /  against HTML in       |  Send 20 Dollars for more info.
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://home.arcor.de/luethje/prog/

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

Search



Quick Links

User menu

Not signed in.

Misc Menu