1. RE: Short-cut evaluation Question
- Posted by "CoJaBo" <CoJaBo at suscom.net> Feb 29, 2004
- 403 views
It might be thinking: o = (0 or TypesMatch(o, "QWidget") or IsBaseClass(o,"QWidget"))) So Try: 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 > -----Original Message----- > From: Travis Beaty [mailto:twbeaty at osage.net] > Sent: Sunday, February 29, 2004 3:01 PM > To: EUforum at topica.com > Subject: Short-cut evaluation Question > > > Hello everyone! > > 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, > > Travis W. Beaty > Osage, Iowa. > > -- > Infancy, n.: > The period of our lives when, according to Wordsworth, "Heaven lies > about us." The world begins lying about us pretty soon afterward. > -- Ambrose Bierce >
2. RE: Short-cut evaluation Question
- Posted by "Derek Parnell" <ddparnell at bigpond.com> Mar 01, 2004
- 362 views
> -----Original Message----- > From: Travis Beaty [mailto:twbeaty at osage.net] > Subject: Re: Short-cut evaluation Question > > > > Hello again! > > On Sunday 29 February 2004 02:47 pm, Juergen Luethje wrote: > > > 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 > > > Yes, your way of doing it does work. However, now I get > warning messages that > "call to TypesMatch() might be short-circuited." Hmm ... > wouldn't this > typically be the desirable outcome? I suppose the only time > that it wouldn't > be is in someone were depending upon a side effect, and such > a place would be > a rather bad place to do that, since Euphoria uses short-cut > evaluation. > > I've opted for this rather wordy set up instead. > > > global type Widget(object o) > > if not atom(o) then > return false > elsif o = 0 then > return true > else > return (TypesMatch(o, "QWidget") or > IsBaseClass(o, "QWidget")) > end if > > end type > > It seems to work, and without warnings. For what its worth, I prefer this method below because it means that the routine only has a single exiting point, but sometimes one must compromise if not efficient enough. global type Widget(object o) integer rc rc = false if atom(o) then if o = 0 then rc = true elsif TypesMatch(o, "QWidget") rc = true elsif IsBaseClass(o, "QWidget") rc = true end if end if return rc end type > As far as my previous set up ... > > return (o = 0 or TypesMatch(o, "QWidget") or IsBaseClass(o, > "QWidget")) > > Why would short-cut evaluation not be a normal, or even > "Good" thing in this > instance? Well, it could be argued that Euphoria cannot know that there is a dependancy between TypesMatch() and IsBaseClass(). This is why you need to explictly encode this in your application. However, in some cases euphoria could be made smarter... return (i >= 1) and (i <= length(s)) and (s[i] = somevalue) -- Derek