Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 26, 2016 Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
for example: if(any floatingpoint.init) will be true if(any char.init) also true if("") also true while others are false e.g. string s; if(s) will be false all others are also false or did i miss any? |
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote:
> for example:
>
> if(any floatingpoint.init) will be true
> if(any char.init) also true
> if("") also true
>
> while others are false e.g.
>
> string s;
> if(s) will be false
> all others are also false or did i miss any?
It's a shortcut that works for certain type and that means:
- pointers: if (ptr) <=> if (ptr != null)
- pointers: if (!ptr) <=> if (ptr == null)
- integral(*): if (i) <=> if (i > 0)
- integral: if (!i) <=> if (i == 0)
- classes: if (c) <=> if (c !is null)
- classes: if (!c) <=> if (c is null)
(*) integral: generally speaking so: byte, ubyte, short, ushort, int, uint, long, ulong, char, wchar, dchar and also, very special case, structs with an alias this to one of this integral type.
for array this works and this tests the (.ptr) member but most of the people here (incl. me) would recommand rather to always do:
"if (arr.length)"
because in some cases "if (arr)" will yield "true" even if the length is equal to 0.
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:11:50 UTC, Basile B. wrote: > On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote: >> [...] > [...] > - integral(*): if (i) <=> if (i > 0) I obviously meant: - integral(*): if (i) <=> if (i <> 0) and "<=>" stands for "equivalence" |
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:14:21 UTC, Basile B. wrote:
> On Thursday, 26 May 2016 at 15:11:50 UTC, Basile B. wrote:
>> On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote:
>>> [...]
>
>> [...]
>> - integral(*): if (i) <=> if (i > 0)
>
> I obviously meant:
>
> - integral(*): if (i) <=> if (i <> 0)
>
> and "<=>" stands for "equivalence"
I obviously meant:
integral(*): if (i) <=> if (i != 0),
"<>" is the Pascal operator for C's "!=" ....
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:15:57 UTC, Basile B. wrote:
> On Thursday, 26 May 2016 at 15:14:21 UTC, Basile B. wrote:
>> On Thursday, 26 May 2016 at 15:11:50 UTC, Basile B. wrote:
>>> On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote:
>>>> [...]
>>
>>> [...]
>>> - integral(*): if (i) <=> if (i > 0)
>>
>> I obviously meant:
>>
>> - integral(*): if (i) <=> if (i <> 0)
>>
>> and "<=>" stands for "equivalence"
>
> I obviously meant:
>
> integral(*): if (i) <=> if (i != 0),
>
> "<>" is the Pascal operator for C's "!=" ....
yes i know about most of those shortcuts its just float.init and char.init that work different then the other when you do this
if(someType) // will be true for float and char while someType is T.init
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On 05/26/2016 04:03 PM, ArturG wrote:
> for example:
>
> if(any floatingpoint.init) will be true
> if(any char.init) also true
> if("") also true
>
> while others are false e.g.
>
> string s;
> if(s) will be false
> all others are also false or did i miss any?
What does it matter?
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Thursday, 26 May 2016 at 15:25:26 UTC, ag0aep6g wrote:
> On 05/26/2016 04:03 PM, ArturG wrote:
>> for example:
>>
>> if(any floatingpoint.init) will be true
>> if(any char.init) also true
>> if("") also true
>>
>> while others are false e.g.
>>
>> string s;
>> if(s) will be false
>> all others are also false or did i miss any?
>
> What does it matter?
You would have to create special cases for them.
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to arturg | On Thursday, 26 May 2016 at 15:25:03 UTC, arturg wrote:
> On Thursday, 26 May 2016 at 15:15:57 UTC, Basile B. wrote:
>> On Thursday, 26 May 2016 at 15:14:21 UTC, Basile B. wrote:
>>> On Thursday, 26 May 2016 at 15:11:50 UTC, Basile B. wrote:
>>>> On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote:
>>>>> [...]
>>>
>>>> [...]
>>>> - integral(*): if (i) <=> if (i > 0)
>>>
>>> I obviously meant:
>>>
>>> - integral(*): if (i) <=> if (i <> 0)
>>>
>>> and "<=>" stands for "equivalence"
>>
>> I obviously meant:
>>
>> integral(*): if (i) <=> if (i != 0),
>>
>> "<>" is the Pascal operator for C's "!=" ....
>
> yes i know about most of those shortcuts its just float.init and char.init that work different then the other when you do this
>
> if(someType) // will be true for float and char while someType is T.init
float.init is not equal to 0.0f. In D FP points values are initialized to nan (not a number).
By the way for strings it works, it's like the array case I described in the first answer).
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:29:52 UTC, Basile B. wrote:
>
> float.init is not equal to 0.0f. In D FP points values are initialized to nan (not a number).
>
> By the way for strings it works, it's like the array case I described in the first answer).
yes i guess i tested all/most types and know that float.init is float.nan but why is nan true and not false?
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Thursday, 26 May 2016 at 15:34:50 UTC, ArturG wrote:
> On Thursday, 26 May 2016 at 15:29:52 UTC, Basile B. wrote:
>
>>
>> float.init is not equal to 0.0f. In D FP points values are initialized to nan (not a number).
>>
>> By the way for strings it works, it's like the array case I described in the first answer).
>
> yes i guess i tested all/most types and know that float.init is float.nan but why is nan true and not false?
because nan is not 0 and that the shortcut for float is
if (fpValue) <=> if (fpValue != 0)
if (!fpValue)<=> if (fpValue == 0)
There's no relation between the initializer and the shortcut. It's not because for some values the shortcut matches to the initializer that it must always be the case...But I admit I don't know the exact rationale.
Does anyone know ?
|
Copyright © 1999-2021 by the D Language Foundation