| Thread overview |
|---|
April 06, 2009 minimal evaluation | ||||
|---|---|---|---|---|
| ||||
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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | >
> 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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | 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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs |
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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | 在 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 Re: minimal evalu-OMG COOKIE YAY | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | 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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | 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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to TomD | 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 Re: minimal evaluation | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply