String to boolean inconsistency

Simen kjaeraas simen.kjaras at gmail.com
Sat Dec 11 19:00:36 PST 2010


Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:

> Actually I'm having a hard time understanding this:
>
> void main()
> {
> string s = "";
> assert(s);  // pass, but why?
> assert(s !is null);  // pass
> }
>
> void main()
> {
> string s = "".idup;
> assert(s);  // fail
> assert(s !is null);  // pass
> }


Try adding writeln( s.ptr ); in there. Should probably give you an
indication of what is and == do.

Answer:

== null:
      Length
P    ==0 !=0
t ==0 T   F
r !=0 T   F


is null:
      Length
P    ==0 !=0
t ==0 T   F
r !=0 F   F

Where T and F stand for true and false, respectively.


So likely, idup on an empty string returns an array with null ptr and
0 length, while "" is 'allocated' in the data segment, and thus given a
ptr value.

-- 
Simen


More information about the Digitalmars-d mailing list