www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - typeid() after const/immutable qualifiers

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
In TDPL, page 289 & 299, there's this code (I've modified the names slightly)
and explanation:

struct A
{
    const(int[]) c;
    immutable(int[]) i;
}

import std.stdio;

unittest
{
    const(A) const_a;
    immutable(A) immu_b;
}

A short version of the explanation: 

"if qualifiers would apply blindly, the types would be:

const_a.c == const(const(int[]))
const_a.i == const(immutable(int[]))

immu_b.c == immutable(const(int[])).
immu_b.i == immutable(immutable(int[])).

When two qualifiers are identical, they are collapsed into one, otherwise
const(immutable(T)) and immutable(const(T)) are both collapsed into
immutable(T)"

From my interpretation, that would mean the types are now:

const_a.c == const(int[])
const_a.i == immutable(int[])

immu_b.c == immutable(int[])
immu_b.i == immutable(int[])

Am I correct so far?

Well first of all, DMD doesn't actually print it out simple qualifiers when
arrays are used, for example:

const(int[]) x;
writeln(typeid(x));

Writes: 
const(const(int)[])

Which is fine, both x and it's contents are const so it's the correct output. 

The second thing which I'm actually puzzled by, is why I'm getting typeid()
return the same qualifiers as defined in the struct. Here's some simplified
code with using basic types, not arrays:

struct A
{
    const(int) c;
    immutable(int) i;
}

import std.stdio;

unittest
{
    const(A) const_a;
    immutable(A) immu_b;
    
    writeln("const_a.c == ", typeid(const_a.c));
    writeln("const_a.i == ", typeid(const_a.i));
    
    writeln("immu_b.c == ", typeid(immu_b.c));
    writeln("immu_b.i == ", typeid(immu_b.i));
}

void main()
{

}

Writes:
const_a.c == const(int)
const_a.i == immutable(int)

immu_b.c == const(int)
immu_b.i == immutable(int)

Shouldn't this be this instead:
const_a.c == const(int)
const_a.i == immutable(int)

immu_b.c == immutable(int)  // immu_b.c is now immutable
immu_b.i == immutable(int)

AFAIK immutable propagates to all fields of the struct, so const c should be an
immutable now?
Aug 19 2010
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Btw, should I skip trying to use inout at all for now? I've read some posts
saying that it's awfully broken, and the example of inout in TDPL doesn't work..


Andrej Mitrovic Wrote:

 snip

Aug 19 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 19 Aug 2010 12:47:45 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Btw, should I skip trying to use inout at all for now? I've read some  
 posts saying that it's awfully broken, and the example of inout in TDPL  
 doesn't work..

Yes. I have expressed the cases that inout should deal with in bug http://d.puremagic.com/issues/show_bug.cgi?id=3748 -Steve
Aug 23 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:
 Well first of all, DMD doesn't actually print it out simple qualifiers when
arrays are used, for example:

I have an open bug report on this. Bye, bearophile
Aug 19 2010
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Good to hear. I was almost going to open an enhancement request. :)

bearophile Wrote:

 Andrej Mitrovic:
 Well first of all, DMD doesn't actually print it out simple qualifiers when
arrays are used, for example:

I have an open bug report on this. Bye, bearophile

Aug 19 2010