Jump to page: 1 2 3
Thread overview
Why do some T.init evaluate to true while others to false?
May 26, 2016
ArturG
May 26, 2016
Basile B.
May 26, 2016
Basile B.
May 26, 2016
Basile B.
May 26, 2016
arturg
May 26, 2016
Basile B.
May 26, 2016
ArturG
May 26, 2016
Basile B.
May 26, 2016
ArturG
May 26, 2016
Basile B.
May 26, 2016
Basile B.
May 26, 2016
ArturG
May 27, 2016
Marc Schütz
May 27, 2016
ArturG
May 27, 2016
Adam D. Ruppe
May 27, 2016
ArturG
May 27, 2016
Adam D. Ruppe
May 27, 2016
ArturG
May 27, 2016
Adam D. Ruppe
May 27, 2016
ArturG
May 27, 2016
ArturG
May 27, 2016
ArturG
May 30, 2016
Alex Parrill
May 30, 2016
ArturG
May 31, 2016
Marc Schütz
May 26, 2016
ag0aep6g
May 26, 2016
ArturG
May 26, 2016
ag0aep6g
May 26, 2016
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
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
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
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
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
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
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
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
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
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 ?
« First   ‹ Prev
1 2 3