www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Typeinfo

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Apr 02 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Apr 02 2015
prev sibling next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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?
Apr 02 2015
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Apr 02 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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.
Apr 02 2015
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Apr 02 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Apr 03 2015
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Apr 03 2015
parent reply "Dicebot" <public dicebot.lv> writes:
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!
Apr 03 2015
parent "bitwise" <bitwise.pvt gmail.com> writes:
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
Apr 03 2015