www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - minimal evaluation

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Hi All,

Is minimal evaluation always enabled in D?

I want to write a function IsNull(), so that I can check the precondition as
follows:

  if (isNull(foo) ||
      isNull(foo.getBar) ||
      isNull(foo.getBar.getBar2)
  {
    return false;
  }
  // normal code goes here

If an argument is null, the IsNull() will return false. Internally it will
be logged to console as well.

But I am not sure, if dmd-compiler generates code without minimal evaluation
in some cases. If minimal evaluation is not always enabled. I cannot do
precodition check in my way. 

--Qian
Apr 06 2009
next sibling parent reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
 
 if (isNull(foo) ||
     isNull(foo.getBar) ||
     isNull(foo.getBar.getBar2)
 {
   return false;
 }
Update: If minimal evaluation is not always enabled, and foo.getBar is NULL. I will get a segfault when evaluating foo.getBar.getBar2.
Apr 06 2009
parent davidl <davidl nospam.org> writes:
ÔÚ Mon, 06 Apr 2009 19:33:31 +0800£¬Qian Xu <quian.xu stud.tu-ilmenau.de>  
дµÀ:

 if (isNull(foo) ||
     isNull(foo.getBar) ||
     isNull(foo.getBar.getBar2)
 {
   return false;
 }
Update: If minimal evaluation is not always enabled, and foo.getBar is NULL. I will get a segfault when evaluating foo.getBar.getBar2.
huh? post the whole code plz. It should work. -- ʹÓà Opera ¸ïÃüÐԵĵç×ÓÓʼþ¿Í»§³ÌÐò: http://www.opera.com/mail/
Apr 06 2009
prev sibling next sibling parent reply downs <default_357-line yahoo.de> writes:
Qian Xu wrote:
 Hi All,
 
 Is minimal evaluation always enabled in D?
 
 I want to write a function IsNull(), so that I can check the precondition as
 follows:
 
   if (isNull(foo) ||
       isNull(foo.getBar) ||
       isNull(foo.getBar.getBar2)
   {
     return false;
   }
   // normal code goes here
 
 If an argument is null, the IsNull() will return false. Internally it will
 be logged to console as well.
 
 But I am not sure, if dmd-compiler generates code without minimal evaluation
 in some cases. If minimal evaluation is not always enabled. I cannot do
 precodition check in my way. 
 
 --Qian
Short-circuit evaluation is always enabled. http://digitalmars.com/d/1.0/expression.html#OrOrExpression "If the left operand, converted to type bool, evaluates to true, then the right operand is not evaluated."
Apr 06 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
downs wrote:
 Qian Xu wrote:
 Hi All,

 Is minimal evaluation always enabled in D?

 I want to write a function IsNull(), so that I can check the precondition as
 follows:

   if (isNull(foo) ||
       isNull(foo.getBar) ||
       isNull(foo.getBar.getBar2)
   {
     return false;
   }
   // normal code goes here

 If an argument is null, the IsNull() will return false. Internally it will
 be logged to console as well.

 But I am not sure, if dmd-compiler generates code without minimal evaluation
 in some cases. If minimal evaluation is not always enabled. I cannot do
 precodition check in my way. 

 --Qian
Short-circuit evaluation is always enabled. http://digitalmars.com/d/1.0/expression.html#OrOrExpression "If the left operand, converted to type bool, evaluates to true, then the right operand is not evaluated."
You beat me to it, AND you found it in the spec. ?cookie downs -- Daniel
Apr 06 2009
parent downs <default_357-line yahoo.de> writes:
Daniel Keep wrote:
 
 downs wrote:
 Qian Xu wrote:
 Hi All,

 Is minimal evaluation always enabled in D?

 I want to write a function IsNull(), so that I can check the precondition as
 follows:

   if (isNull(foo) ||
       isNull(foo.getBar) ||
       isNull(foo.getBar.getBar2)
   {
     return false;
   }
   // normal code goes here

 If an argument is null, the IsNull() will return false. Internally it will
 be logged to console as well.

 But I am not sure, if dmd-compiler generates code without minimal evaluation
 in some cases. If minimal evaluation is not always enabled. I cannot do
 precodition check in my way. 

 --Qian
Short-circuit evaluation is always enabled. http://digitalmars.com/d/1.0/expression.html#OrOrExpression "If the left operand, converted to type bool, evaluates to true, then the right operand is not evaluated."
You beat me to it, AND you found it in the spec. ?cookie downs
Ooh a cookie! :D :munch:
 
   -- Daniel
Apr 06 2009
prev sibling parent reply TomD <t_demmer nospam.web.de> writes:
Qian Xu Wrote:

 Hi All,
 
 Is minimal evaluation always enabled in D?
 
 I want to write a function IsNull(), so that I can check the precondition as
 follows:
 
   if (isNull(foo) ||
       isNull(foo.getBar) ||
       isNull(foo.getBar.getBar2)
   {
     return false;
   }
   // normal code goes here
 
 If an argument is null, the IsNull() will return false. Internally it will
 be logged to console as well.
Make it return true then. An "or" is true when it hits the first true expression. Ciao TomD
Apr 06 2009
parent reply downs <default_357-line yahoo.de> writes:
TomD wrote:
 Qian Xu Wrote:
 
 Hi All,

 Is minimal evaluation always enabled in D?

 I want to write a function IsNull(), so that I can check the precondition as
 follows:

 Â  if (isNull(foo) ||
 Â  Â  Â  isNull(foo.getBar) ||
 Â  Â  Â  isNull(foo.getBar.getBar2)
 Â  {
 Â  Â  return false;
 Â  }
 Â  // normal code goes here

 If an argument is null, the IsNull() will return false. Internally it will
 be logged to console as well.
Make it return true then. An "or" is true when it hits the first true expression.
.. Wait. Did I just read that right - when an argument is null, isNull returns *false*? What the hell?
 Ciao
 TomD
Apr 07 2009
parent TomD <t_demmer nospam.web.de> writes:
downs Wrote:

[...]
  .. Wait. Did I just read that right - when an argument is null, isNull
returns *false*?
 
 What the hell?
At least that is what he had written. Seems to be a case of "double negation is still a negation", an idiom quite common here in Bavaria. Not in programming, though. :-) Ciao TomD
Apr 07 2009