www.digitalmars.com         C & C++   DMDScript  

D - type as expression

reply Chris Sauls <ibisbasenji yahoo.com> writes:
We have the typeof() decleration now, and that's great, but there's 
still one thing that I can't do (correct me if I'm wrong) that I'd like 
to see, and that's something like:

if (typeof(foo) == int)

I don't know how it could be implemented, though.  Maybe typeof() could 
take an optional second parameter, as a type keyword, and return a 
boolean value in that usage?  Then this would be:

if (typeof(foo, int))

Seems decent, even if inconsistant.  I know that in Lux the type 
keywords are really language-defined constants, and can be used anywhere 
a 32-bit integer is legal.  In other words, in Lux the following two are 
equivelant:

new int { foo = 32; }
new 288 { foo = 32; } // 288 is the actual value of the 'int' constant

So then I can do this check in Lux with:

if (foo:type == int)

And all the world rejoiced.  So is there a way to do this same check 
easily in D, and if not, can there be?  Are D types mapped to integer 
constants at all, which would make this doable at runtime?  Just a 
thought.  Mind you that I've been missing sleep, so who knows how far 
off base I might be.  And yes I'm still working on the persistance 
lib... I paused it while DMD goes through all these fun changes, and to 
work out an issue with arrays.  More on that later, though.

Thoughts?

  - Chris S.
  - Invironz
Jan 22 2004
next sibling parent reply Matthias Becker <Matthias_member pathlink.com> writes:
We have the typeof() decleration now, and that's great, but there's 
still one thing that I can't do (correct me if I'm wrong) that I'd like 
to see, and that's something like:

if (typeof(foo) == int)
You can do it by template specialisation.
I don't know how it could be implemented, though.  Maybe typeof() could 
take an optional second parameter, as a type keyword, and return a 
boolean value in that usage?  Then this would be:

if (typeof(foo, int))
Why should the language be extended this way? It's easy to do it now. (I haven't tried it, but as the template-abilitys are pretty good this shouldn't be a problem, as the only thing that's missing compared to C++ are member templates, which you don't need here.)
Jan 22 2004
parent reply "Walter" <walter digitalmars.com> writes:
"Matthias Becker" <Matthias_member pathlink.com> wrote in message
news:bup87c$2uin$1 digitaldaemon.com...
 Why should the language be extended this way? It's easy to do it now. (I
haven't
 tried it, but as the template-abilitys are pretty good this shouldn't be a
 problem, as the only thing that's missing compared to C++ are member
templates,
 which you don't need here.)
Member templates should work fine in D.
Jan 22 2004
parent Matthias Becker <Matthias_member pathlink.com> writes:
Member templates should work fine in D.
I haven't tied it, but you documentation says: "Templates cannot be used to add non-static members or functions to classes." I'm sorry, but I didn't kknow that this isn't valid anymore (as it's still in the documentation).
Jan 24 2004
prev sibling parent Roel Mathys <roel.mathys yucom.be> writes:
one solution, that works with the current compiler

bye,
roel

/+ --------------------------------------- +/

template eqType(T,U)
{
   const bit value = false;
}

template eqType(T,U:T)
{
   const bit value = true;
}

int main()
{
   int i = 5;
   if ( eqType!(typeof(i),int).value)
     printf( "Twice the same!" );
   else
     printf( "Not the same!" );

   printf(\n);
   return 0;
}
Jan 24 2004