D - type as expression
- Chris Sauls (26/26) Jan 22 2004 We have the typeof() decleration now, and that's great, but there's
- Matthias Becker (5/13) Jan 22 2004 Why should the language be extended this way? It's easy to do it now. (I...
- Walter (5/9) Jan 22 2004 haven't
- Matthias Becker (4/5) Jan 24 2004 I haven't tied it, but you documentation says:
- Roel Mathys (22/22) Jan 24 2004 one solution, that works with the current compiler
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
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
"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. (Ihaven'ttried 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 membertemplates,which you don't need here.)Member templates should work fine in D.
Jan 22 2004
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
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









Matthias Becker <Matthias_member pathlink.com> 