Thread overview
Typeinfo
Apr 03, 2015
Adam D. Ruppe
Apr 03, 2015
Rikki Cattermole
Apr 03, 2015
Rikki Cattermole
Apr 03, 2015
Dicebot
Apr 03, 2015
bitwise
April 03, 2015
Hey folks, is there any way to figure out whether a type is immutable or shared given its typeinfo? I see there's only the flags() method that doesn't tell that. I'm thinking we'd do good to extend that.

This is needed for allocators. I'm thinking an allocator might be interested at creation time to use different allocation strategies for qualified types, especially shared ones.


Thanks,

Andrei
April 03, 2015
On Friday, 3 April 2015 at 00:21:47 UTC, Andrei Alexandrescu wrote:
> Hey folks, is there any way to figure out whether a type is immutable or shared given its typeinfo?

Yes: if it is an instance of TypeInfo_Invariant or TypeInfo_Shared.

void main() {
        string s;
        char[] c;
        assert(cast(TypeInfo_Invariant) typeid(s).next !is null);
        assert(cast(TypeInfo_Invariant) typeid(c).next is null);
}

The next is there because the array itself isn't immutable, so we look at the type inside.
April 03, 2015
On 3/04/2015 1:21 p.m., Andrei Alexandrescu wrote:
> Hey folks, is there any way to figure out whether a type is immutable or
> shared given its typeinfo? I see there's only the flags() method that
> doesn't tell that. I'm thinking we'd do good to extend that.
>
> This is needed for allocators. I'm thinking an allocator might be
> interested at creation time to use different allocation strategies for
> qualified types, especially shared ones.
>
>
> Thanks,
>
> Andrei

Small question, how exactly are you getting the TypeInfo without knowing its exact type at CT?
April 03, 2015
On 4/2/15 5:52 PM, Rikki Cattermole wrote:
> Small question, how exactly are you getting the TypeInfo without knowing
> its exact type at CT?

I just use it at construction, when the type is known. -- Andrei
April 03, 2015
On 3/04/2015 5:34 p.m., Andrei Alexandrescu wrote:
> On 4/2/15 5:52 PM, Rikki Cattermole wrote:
>> Small question, how exactly are you getting the TypeInfo without knowing
>> its exact type at CT?
>
> I just use it at construction, when the type is known. -- Andrei

Ugh:

void main() {
        alias TYPE = immutable(ubyte[]);

        auto TYPEV2 = cast(immutable)TYPE.init;
        alias TYPE2 = typeof(TYPEV2);

        pragma(msg, TYPE);
        pragma(msg, TYPE2);

        static if (is(TYPE2 == TYPE)) {
                pragma(msg, "immutable");
        } else {
                pragma(msg, "mutable");
        }
}

cast() should be clearing away the immutable, but for some reason it is not working the way I would expect.
April 03, 2015
On 4/2/15 9:58 PM, Rikki Cattermole wrote:
> On 3/04/2015 5:34 p.m., Andrei Alexandrescu wrote:
>> On 4/2/15 5:52 PM, Rikki Cattermole wrote:
>>> Small question, how exactly are you getting the TypeInfo without knowing
>>> its exact type at CT?
>>
>> I just use it at construction, when the type is known. -- Andrei
>
> Ugh:
>
> void main() {
>          alias TYPE = immutable(ubyte[]);
>
>          auto TYPEV2 = cast(immutable)TYPE.init;
>          alias TYPE2 = typeof(TYPEV2);
>
>          pragma(msg, TYPE);
>          pragma(msg, TYPE2);
>
>          static if (is(TYPE2 == TYPE)) {
>                  pragma(msg, "immutable");
>          } else {
>                  pragma(msg, "mutable");
>          }
> }
>
> cast() should be clearing away the immutable, but for some reason it is
> not working the way I would expect.

Loosely related: https://issues.dlang.org/show_bug.cgi?id=14401 -- Andrei

April 03, 2015
On 4/2/15 8:21 PM, Andrei Alexandrescu wrote:
> Hey folks, is there any way to figure out whether a type is immutable or
> shared given its typeinfo? I see there's only the flags() method that
> doesn't tell that. I'm thinking we'd do good to extend that.
>
> This is needed for allocators. I'm thinking an allocator might be
> interested at creation time to use different allocation strategies for
> qualified types, especially shared ones.

I use this technique in lifetime.d:

// typeof(ti) is TypeInfo
auto isshared = typeid(ti) is typeid(TypeInfo_Shared);

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L640

-Steve
April 03, 2015
On 4/3/15 4:53 AM, Steven Schveighoffer wrote:
> On 4/2/15 8:21 PM, Andrei Alexandrescu wrote:
>> Hey folks, is there any way to figure out whether a type is immutable or
>> shared given its typeinfo? I see there's only the flags() method that
>> doesn't tell that. I'm thinking we'd do good to extend that.
>>
>> This is needed for allocators. I'm thinking an allocator might be
>> interested at creation time to use different allocation strategies for
>> qualified types, especially shared ones.
>
> I use this technique in lifetime.d:
>
> // typeof(ti) is TypeInfo
> auto isshared = typeid(ti) is typeid(TypeInfo_Shared);
>
> https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L640

Thanks Adam and Steve. Guess I should have asked this in the learn forum :o). -- Andrei

April 03, 2015
On Friday, 3 April 2015 at 17:51:33 UTC, Andrei Alexandrescu wrote:
> Thanks Adam and Steve. Guess I should have asked this in the learn forum :o). -- Andrei

Yeah, I have totally expected some revolutionary proposal for RTTI improvement in D from you when opening the topic :( You have broken my heart!
April 03, 2015
On Friday, 3 April 2015 at 17:58:27 UTC, Dicebot wrote:
> On Friday, 3 April 2015 at 17:51:33 UTC, Andrei Alexandrescu wrote:
>> Thanks Adam and Steve. Guess I should have asked this in the learn forum :o). -- Andrei
>
> Yeah, I have totally expected some revolutionary proposal for RTTI improvement in D from you when opening the topic :( You have broken my heart!

+1