RE: Short-cut evaluation Question

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

> -----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

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

Search



Quick Links

User menu

Not signed in.

Misc Menu