www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the base type of a typedef

reply MLT <none anone.com> writes:
Another question about typedef...

I am using 
typedef long location ;

Now I would like to use tango.math.random.Random. But I am having problems with
calling r.uniformR( hi ) when r is a random genarator, and hi is of type
location.
I need to call 
r.uniform!(long)( cast(long)(hi) ) ;

This is ugly, and will work only as long as location remains long. If I decide
to change location to 'int', I'll have to go and edit all of these casts.
Is there a better way to do this?
Is there a way to get the base type of location? Something like 
typeof(hi), but that returns 'long' when location is long, and 'int' when int?

thanks!
May 01 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
MLT:
 Is there a way to get the base type of location?
See the BaseTypedef() template in the "templates" module of my dlibs (they are for Phobos): http://www.fantascienza.net/leonardo/so/libs_d.zip Bye, bearophile
May 01 2009
next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 MLT:
 Is there a way to get the base type of location?
See the BaseTypedef() template in the "templates" module of my dlibs (they are for Phobos): http://www.fantascienza.net/leonardo/so/libs_d.zip Bye, bearophile
It's probably something along the lines of: template BaseTypedef(T) { static if( is( T U == typedef ) ) alias U BaseTypedef; else alias T BaseTypedef; } -- Daniel
May 01 2009
prev sibling parent reply MLT <none anone.com> writes:
Thank you very much!
I'm using tango, and have a problem combining both on OSX. But I just copied
the definition:
template BaseTypedef(T) {
    static if( is( T BaseType1 == typedef ) )
        alias BaseTypedef!(BaseType1) BaseTypedef;
    else
        alias T BaseTypedef;
}

and it works! Great!!
Hmmm... but how?
in "is( T BaseType1 == typedef )" Basetype1 seems to be a variable of type T.
Then calling 'alias BaseTypedef!(BaseType1) BaseTypedef;' does not seem to
strip away a layer... isn't it still called on a variable of type T? Or does
'is()' do some magic? 


bearophile Wrote:

 MLT:
 Is there a way to get the base type of location?
See the BaseTypedef() template in the "templates" module of my dlibs (they are for Phobos): http://www.fantascienza.net/leonardo/so/libs_d.zip Bye, bearophile
May 01 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, May 1, 2009 at 9:54 AM, MLT <none anone.com> wrote:

 and it works! Great!!
 Hmmm... but how?
 in "is( T BaseType1 == typedef )" Basetype1 seems to be a variable of type T.
 Then calling 'alias BaseTypedef!(BaseType1) BaseTypedef;' does not seem to
strip away a layer... isn't it still called on a variable of type T? Or does
'is()' do some magic?
is() does a lot of magic. It has the worst, most confusing thing in the language. When you do is(T U == typedef), it will evaluate to true if T is a typedef, and if so, will declare an alias U which is aliased to the base type of the typedef. It's not a variable, just a type. The recursive call to BaseTypedef!(BaseType1) is there because BaseType1 _itself_ may be a typedef. For instance: typedef int a; typedef a b; If you call BaseTypedef!(b), the is() will figure out that its base type is a; then it recursively calls BaseTypedef!(a), which determines the base type is int; then it recursively calls BaseTypedef!(int), where the is() evaluates to false, it hits the base case of recursion, and evaluates to int. That propagates up, and so BaseTypedef!(b) evaluates to int.
May 01 2009
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
MLT:
But I just copied the definition:<
If you have to copy something, then must always copy the unittests too, they are a part of the code. They aren't there just for show. And usually it's better to also copy the relative ddocs too, they are part of the user interface of a function/template/class/method/etc. Bye, bearophile
May 01 2009