Thread overview
minimal evaluation
Apr 06, 2009
Qian Xu
Apr 06, 2009
Qian Xu
Apr 06, 2009
davidl
Apr 06, 2009
downs
Apr 06, 2009
Daniel Keep
Re: minimal evalu-OMG COOKIE YAY
Apr 06, 2009
downs
Apr 06, 2009
TomD
Apr 07, 2009
downs
Apr 07, 2009
TomD
April 06, 2009
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
April 06, 2009
> 
> 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.


April 06, 2009
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."
April 06, 2009

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
April 06, 2009
在 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/
April 06, 2009
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
April 06, 2009
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
April 07, 2009
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
April 07, 2009
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