digitalmars.D - typeof headache
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "me" <memsom interalpha.co.uk> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- Mike Swieton <mike swieton.net> Jun 30 2004
- Norbert Nemec <Norbert.Nemec gmx.de> Jun 30 2004
- "Bent Rasmussen" <exo bent-rasmussen.info> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Bent Rasmussen" <exo bent-rasmussen.info> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- "Walter" <newshound digitalmars.com> Jun 30 2004
- "Dan Williams" <dnews ithium.NOSPAM.net> Jun 30 2004
- Hauke Duden <H.NS.Duden gmx.net> Jun 30 2004
- "Walter" <newshound digitalmars.com> Jun 30 2004
- Andy Friesen <andy ikagames.com> Jun 30 2004
- "Walter" <newshound digitalmars.com> Jun 30 2004
- Daniel Horn <hellcatv hotmail.com> Jun 30 2004
- Regan Heath <regan netwin.co.nz> Jun 30 2004
I'm having a little trouble with typeof().
The D documentation says: "Typeof is a way to specify a type based on the
type of an expression."
That's fair enough, but in that case how do you DETECT the current type of
an expression?
I was hoping that either typeof would return something to tell me a current
type, or that I could at the very least to typeof comparison checking such
as if (typeof('c') == typeof(int)) - something like that in any case.
However, the compiler keeps yelling that it expects a dot, and I'm really
not sure how to do what I want. Things like typeof(a).sizeof work fine (by
the way, why was size deprecated in favour of sizeof? To my mind, size is
more fitting for a property, i.e. a.size, and sizeof for a function, i.e.
sizeof(a). But never mind...) however the code examples on the D site are
very sparse when it comes to typeof(), and some do not actually work at all.
I guess I'm a little confused about the purpose of typeof(), as my initial
logical assumption would be that it would check what type something is, and
yet the documentation (keyword: "specify") makes me think that it's actually
more like cast().
Which brings me to two questions: if typeof() is actually like cast(), why
is it there? And how can I detect the type of a variable (or expression)?
Sorry if this is a stupid question, but a search of the D newsgroups and a
Google of the site did not reveal anything to help me.
Jun 30 2004
Thinking about it I expect someone will say at some point something like, "D is statically typed, therefore you know the type of all variables already, so why do you need to detect the type?" So I'll quickly point out that in the case of a union or something you may not always know what data type is in use, and typeof would be very useful there. There are other cases too where you may not actually know the data type of a variable. I propose something like a type or typeof property, i.e. a.type or a.typeof, just like there is a.sizeof. Unless there's a better way to do this? "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message news:cbu8rc$2rkn$1 digitaldaemon.com...I'm having a little trouble with typeof(). The D documentation says: "Typeof is a way to specify a type based on the type of an expression." That's fair enough, but in that case how do you DETECT the current type of an expression? I was hoping that either typeof would return something to tell me a
type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like that in any case. However, the compiler keeps yelling that it expects a dot, and I'm really not sure how to do what I want. Things like typeof(a).sizeof work fine (by the way, why was size deprecated in favour of sizeof? To my mind, size is more fitting for a property, i.e. a.size, and sizeof for a function, i.e. sizeof(a). But never mind...) however the code examples on the D site are very sparse when it comes to typeof(), and some do not actually work at
I guess I'm a little confused about the purpose of typeof(), as my initial logical assumption would be that it would check what type something is,
yet the documentation (keyword: "specify") makes me think that it's
more like cast(). Which brings me to two questions: if typeof() is actually like cast(), why is it there? And how can I detect the type of a variable (or expression)? Sorry if this is a stupid question, but a search of the D newsgroups and a Google of the site did not reveal anything to help me.
Jun 30 2004
I was hoping that either typeof would return something to tell me a
type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like that in any case.
I suggest that D could addopt the less ambiguous (and less prone to error) 'is' operator. e.g class Y {} class X : Y {} X x = new X(); Y y = new Y(); if (x is y) ..; //x is either a 'Y' or descends from it - they are under the same branch of the inheritence tree this works equally well for types if (x is X) ...; //as before, is either of that type or a descendent I guess you could d-ify it to : if (x typeof y) ..; but to my eyes that is less readable.
Jun 30 2004
Hmmmm, an 'is' operator. Yes, that might work... but would it allow something like: if (a is int) etc. With typeof(), I would envisage something like; if (typeof(a) == int) if (typeof(a) == some_constant_meaning_int) if (typeof(a) == typeof(int)) ...that kind of thing. But maybe an effective solution is to combine your 'is' operator with my idea for a typeof property: if (a.is(int)) that is admittedly not as readable/semantic as (a is int) however it would not require any extra operators. Of course, the problem is that 'is' then becomes a method and not a property, which is undesirable. Personally I think either (a is int) or a.type are the best options (probably a.typeof rather than a.type seeing as that seems to be the style for D). "me" <memsom interalpha.co.uk> wrote in message news:cbu9ta$2v9p$1 digitaldaemon.com...I was hoping that either typeof would return something to tell me a
type, or that I could at the very least to typeof comparison checking
as if (typeof('c') == typeof(int)) - something like that in any case.
I suggest that D could addopt the less ambiguous (and less prone to error) 'is' operator. e.g class Y {} class X : Y {} X x = new X(); Y y = new Y(); if (x is y) ..; //x is either a 'Y' or descends from it - they are under
same branch of the inheritence tree this works equally well for types if (x is X) ...; //as before, is either of that type or a descendent I guess you could d-ify it to : if (x typeof y) ..; but to my eyes that is less readable.
Jun 30 2004
I just noticed there already is an 'is' operator, so it would seem that one is out... "me" <memsom interalpha.co.uk> wrote in message news:cbu9ta$2v9p$1 digitaldaemon.com...I was hoping that either typeof would return something to tell me a
type, or that I could at the very least to typeof comparison checking
as if (typeof('c') == typeof(int)) - something like that in any case.
I suggest that D could addopt the less ambiguous (and less prone to error) 'is' operator. e.g class Y {} class X : Y {} X x = new X(); Y y = new Y(); if (x is y) ..; //x is either a 'Y' or descends from it - they are under
same branch of the inheritence tree this works equally well for types if (x is X) ...; //as before, is either of that type or a descendent I guess you could d-ify it to : if (x typeof y) ..; but to my eyes that is less readable.
Jun 30 2004
On Wed, 30 Jun 2004 12:58:34 +0100, me wrote:if (x is y) ..; //x is either a 'Y' or descends from it - they are under the same branch of the inheritence tree
That is already implemented by casts. The cast operator returns null if the objects are unrelated: if (cast(y) x) ...; Mike Swieton __ It's kind of fun to do the impossible. - Walt Disney
Jun 30 2004
Did you try typeid?
----
if(typeid(typeof(a)) == typeid(typeof(b))) ...
----
I did not try it.
I recall Walter mentioning long-term-ideas about handling types directly,
but that's certainly post-1.0
Dan Williams wrote:
I'm having a little trouble with typeof().
The D documentation says: "Typeof is a way to specify a type based on the
type of an expression."
That's fair enough, but in that case how do you DETECT the current type of
an expression?
I was hoping that either typeof would return something to tell me a
current type, or that I could at the very least to typeof comparison
checking such as if (typeof('c') == typeof(int)) - something like that in
any case.
However, the compiler keeps yelling that it expects a dot, and I'm really
not sure how to do what I want. Things like typeof(a).sizeof work fine (by
the way, why was size deprecated in favour of sizeof? To my mind, size is
more fitting for a property, i.e. a.size, and sizeof for a function, i.e.
sizeof(a). But never mind...) however the code examples on the D site are
very sparse when it comes to typeof(), and some do not actually work at
all.
I guess I'm a little confused about the purpose of typeof(), as my initial
logical assumption would be that it would check what type something is,
and yet the documentation (keyword: "specify") makes me think that it's
actually more like cast().
Which brings me to two questions: if typeof() is actually like cast(), why
is it there? And how can I detect the type of a variable (or expression)?
Sorry if this is a stupid question, but a search of the D newsgroups and a
Google of the site did not reveal anything to help me.
Jun 30 2004
Did you try typeid? ---- if(typeid(typeof(a)) == typeid(typeof(b))) ... ---- I did not try it.
I did printf("%i",typeid(typeof(0)) === typeid(typeof(0))); printf("\n%i",typeid(typeof(0)) === typeid(typeof(true))); prints 1 0 Notice the use of the "===" operator.
Jun 30 2004
Hmmmm, you know when using cast() you do something like:
bit a;
int b;
b = cast(int) a;
Well, I kinda assumed I could do the same with typeof/typeid/whatever?
i.e.
printf("%i",typeid(typeof(0)) === typeid(typeof(int)));
...maybe I better go play with it a bit first and see what happens :)
"Bent Rasmussen" <exo bent-rasmussen.info> wrote in message
news:cbugq2$7sv$1 digitaldaemon.com...
Did you try typeid?
----
if(typeid(typeof(a)) == typeid(typeof(b))) ...
----
I did not try it.
I did
printf("%i",typeid(typeof(0)) === typeid(typeof(0)));
printf("\n%i",typeid(typeof(0)) === typeid(typeof(true)));
prints
1
0
Notice the use of the "===" operator.
Jun 30 2004
printf("%i",typeid(typeof(0)) === typeid(typeof(int))); ...maybe I better go play with it a bit first and see what happens :)
If I understand this correctly typeof maps from values to types and int is not a value, its a type, hence it doesn't make sense to use typeof on it. Thus use printf("%i",typeid(typeof(0)) === typeid(int)); I'm not quite sure what you're asking for.
Jun 30 2004
You are correct. "Bent Rasmussen" <exo bent-rasmussen.info> wrote in message news:cbujou$chr$1 digitaldaemon.com...printf("%i",typeid(typeof(0)) === typeid(typeof(int))); ...maybe I better go play with it a bit first and see what happens :)
If I understand this correctly typeof maps from values to types and int is not a value, its a type, hence it doesn't make sense to use typeof on it. Thus use printf("%i",typeid(typeof(0)) === typeid(int)); I'm not quite sure what you're asking for.
Jun 30 2004
Gah... typeid? I'll have to go and look that up and play with it. I certainly didn't know there even *was* a typeid! Anyhow... I think the comments that have been made about typeof remain valid. "Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message news:cbue48$3eu$1 digitaldaemon.com...Did you try typeid? ---- if(typeid(typeof(a)) == typeid(typeof(b))) ... ---- I did not try it. I recall Walter mentioning long-term-ideas about handling types directly, but that's certainly post-1.0 Dan Williams wrote:I'm having a little trouble with typeof(). The D documentation says: "Typeof is a way to specify a type based on
type of an expression." That's fair enough, but in that case how do you DETECT the current type
an expression? I was hoping that either typeof would return something to tell me a current type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like that
any case. However, the compiler keeps yelling that it expects a dot, and I'm
not sure how to do what I want. Things like typeof(a).sizeof work fine
the way, why was size deprecated in favour of sizeof? To my mind, size
more fitting for a property, i.e. a.size, and sizeof for a function,
sizeof(a). But never mind...) however the code examples on the D site
very sparse when it comes to typeof(), and some do not actually work at all. I guess I'm a little confused about the purpose of typeof(), as my
logical assumption would be that it would check what type something is, and yet the documentation (keyword: "specify") makes me think that it's actually more like cast(). Which brings me to two questions: if typeof() is actually like cast(),
is it there? And how can I detect the type of a variable (or
Sorry if this is a stupid question, but a search of the D newsgroups and
Google of the site did not reveal anything to help me.
Jun 30 2004
Ok I've been and tested this typeid thing. I know C has typeid, but because
D has typeof I wasn't really expecting typeid and forgot about it.
Now, in C you can use typeid on an expression, but in D that confusingly
does not appear to be the case.
typeid(1) fails.
typeid(typeof(1)) works.
typeid(int) works.
so... I can do:
if (typeid(typeof(1)) == typeid(typeof(0)))...
...just as was suggested, and so too can I do:
if (typeid(typeof(1)) == typeid(int))...
But I cannot do:
if (typeof(1) == int)...
Surely this is more convoluted than it needs to be? As I see it, a lot of
this is overkill.
If typeof returns something that equates to an actual type, can we not use
that ourselves? I mean, typeid(typeof(0))) is the same as typeid(int)),
which means that typeof(0) is int, as expected, but it cannot be used like
that. Strange.
So I tried this:
printf("%d\n", typeid(typeof('c'))); // prints 4257264
printf("%d\n", typeid(typeof(1))); // prints 4257408
printf("%d\n", typeid(int)); // prints 4257408
It would appear, therefore, that some constants are being used, but I cannot
find reference to them and do not know what they would be called.
The flaw in this theory is that:
if (typeid(typeof(1)) == 4257408)
fails miserably. It would appear that objects are being used somewhere for
some reason, so I give up.
Does this mean that I *have* to use (typeid(typeof(a)) == typeid(int)), or
is there an alternative method?
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message
news:cbue48$3eu$1 digitaldaemon.com...
Did you try typeid?
----
if(typeid(typeof(a)) == typeid(typeof(b))) ...
----
I did not try it.
I recall Walter mentioning long-term-ideas about handling types directly,
but that's certainly post-1.0
Dan Williams wrote:
I'm having a little trouble with typeof().
The D documentation says: "Typeof is a way to specify a type based on
type of an expression."
That's fair enough, but in that case how do you DETECT the current type
an expression?
I was hoping that either typeof would return something to tell me a
current type, or that I could at the very least to typeof comparison
checking such as if (typeof('c') == typeof(int)) - something like that
any case.
However, the compiler keeps yelling that it expects a dot, and I'm
not sure how to do what I want. Things like typeof(a).sizeof work fine
the way, why was size deprecated in favour of sizeof? To my mind, size
more fitting for a property, i.e. a.size, and sizeof for a function,
sizeof(a). But never mind...) however the code examples on the D site
very sparse when it comes to typeof(), and some do not actually work at
all.
I guess I'm a little confused about the purpose of typeof(), as my
logical assumption would be that it would check what type something is,
and yet the documentation (keyword: "specify") makes me think that it's
actually more like cast().
Which brings me to two questions: if typeof() is actually like cast(),
is it there? And how can I detect the type of a variable (or
Sorry if this is a stupid question, but a search of the D newsgroups and
Google of the site did not reveal anything to help me.
Jun 30 2004
typeid returns an instance of class TypeInfo corresponding to the type of
typeid's argument. For example:
typeid(int)
returns an instance of TypeInfo corresponding to int. If two TypeInfo's
represent the same type, then their TypeInfo's will compare equal.
The reason you must do:
typeid(typeof(0)) == typeid(int)
instead of:
typeof(0) == int
is because of parsing ambiguities. Types and expressions follow different
grammar rules, and if there wasn't an easy way to distinguish them, it
cannot be parsed. For example:
foo[]
is that a type array of foo's, or is it the array foo?
"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
news:cbuk8c$d8o$1 digitaldaemon.com...
Ok I've been and tested this typeid thing. I know C has typeid, but
D has typeof I wasn't really expecting typeid and forgot about it.
Now, in C you can use typeid on an expression, but in D that confusingly
does not appear to be the case.
typeid(1) fails.
typeid(typeof(1)) works.
typeid(int) works.
so... I can do:
if (typeid(typeof(1)) == typeid(typeof(0)))...
...just as was suggested, and so too can I do:
if (typeid(typeof(1)) == typeid(int))...
But I cannot do:
if (typeof(1) == int)...
Surely this is more convoluted than it needs to be? As I see it, a lot of
this is overkill.
If typeof returns something that equates to an actual type, can we not use
that ourselves? I mean, typeid(typeof(0))) is the same as typeid(int)),
which means that typeof(0) is int, as expected, but it cannot be used like
that. Strange.
So I tried this:
printf("%d\n", typeid(typeof('c'))); // prints 4257264
printf("%d\n", typeid(typeof(1))); // prints 4257408
printf("%d\n", typeid(int)); // prints 4257408
It would appear, therefore, that some constants are being used, but I
find reference to them and do not know what they would be called.
The flaw in this theory is that:
if (typeid(typeof(1)) == 4257408)
fails miserably. It would appear that objects are being used somewhere for
some reason, so I give up.
Does this mean that I *have* to use (typeid(typeof(a)) == typeid(int)), or
is there an alternative method?
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message
news:cbue48$3eu$1 digitaldaemon.com...
Did you try typeid?
----
if(typeid(typeof(a)) == typeid(typeof(b))) ...
----
I did not try it.
I recall Walter mentioning long-term-ideas about handling types
but that's certainly post-1.0
Dan Williams wrote:
I'm having a little trouble with typeof().
The D documentation says: "Typeof is a way to specify a type based on
type of an expression."
That's fair enough, but in that case how do you DETECT the current
of
an expression?
I was hoping that either typeof would return something to tell me a
current type, or that I could at the very least to typeof comparison
checking such as if (typeof('c') == typeof(int)) - something like that
any case.
However, the compiler keeps yelling that it expects a dot, and I'm
not sure how to do what I want. Things like typeof(a).sizeof work fine
the way, why was size deprecated in favour of sizeof? To my mind, size
more fitting for a property, i.e. a.size, and sizeof for a function,
sizeof(a). But never mind...) however the code examples on the D site
very sparse when it comes to typeof(), and some do not actually work
all.
I guess I'm a little confused about the purpose of typeof(), as my
logical assumption would be that it would check what type something
and yet the documentation (keyword: "specify") makes me think that
actually more like cast().
Which brings me to two questions: if typeof() is actually like cast(),
is it there? And how can I detect the type of a variable (or
Sorry if this is a stupid question, but a search of the D newsgroups
a
Google of the site did not reveal anything to help me.
Jun 30 2004
Ahhhh, excellent, that makes sense :) Thankyou for clearing that up! "Walter" <newshound digitalmars.com> wrote in message news:cbuqpf$mtp$1 digitaldaemon.com...typeid returns an instance of class TypeInfo corresponding to the type of typeid's argument. For example: typeid(int) returns an instance of TypeInfo corresponding to int. If two TypeInfo's represent the same type, then their TypeInfo's will compare equal. The reason you must do: typeid(typeof(0)) == typeid(int) instead of: typeof(0) == int is because of parsing ambiguities. Types and expressions follow different grammar rules, and if there wasn't an easy way to distinguish them, it cannot be parsed. For example: foo[] is that a type array of foo's, or is it the array foo? "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message news:cbuk8c$d8o$1 digitaldaemon.com...Ok I've been and tested this typeid thing. I know C has typeid, but
D has typeof I wasn't really expecting typeid and forgot about it. Now, in C you can use typeid on an expression, but in D that confusingly does not appear to be the case. typeid(1) fails. typeid(typeof(1)) works. typeid(int) works. so... I can do: if (typeid(typeof(1)) == typeid(typeof(0)))... ...just as was suggested, and so too can I do: if (typeid(typeof(1)) == typeid(int))... But I cannot do: if (typeof(1) == int)... Surely this is more convoluted than it needs to be? As I see it, a lot
this is overkill. If typeof returns something that equates to an actual type, can we not
that ourselves? I mean, typeid(typeof(0))) is the same as typeid(int)), which means that typeof(0) is int, as expected, but it cannot be used
that. Strange. So I tried this: printf("%d\n", typeid(typeof('c'))); // prints 4257264 printf("%d\n", typeid(typeof(1))); // prints 4257408 printf("%d\n", typeid(int)); // prints 4257408 It would appear, therefore, that some constants are being used, but I
find reference to them and do not know what they would be called. The flaw in this theory is that: if (typeid(typeof(1)) == 4257408) fails miserably. It would appear that objects are being used somewhere
some reason, so I give up. Does this mean that I *have* to use (typeid(typeof(a)) == typeid(int)),
is there an alternative method? "Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message news:cbue48$3eu$1 digitaldaemon.com...Did you try typeid? ---- if(typeid(typeof(a)) == typeid(typeof(b))) ... ---- I did not try it. I recall Walter mentioning long-term-ideas about handling types
but that's certainly post-1.0 Dan Williams wrote:I'm having a little trouble with typeof(). The D documentation says: "Typeof is a way to specify a type based
thetype of an expression." That's fair enough, but in that case how do you DETECT the current
ofan expression? I was hoping that either typeof would return something to tell me a current type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like
inany case. However, the compiler keeps yelling that it expects a dot, and I'm
not sure how to do what I want. Things like typeof(a).sizeof work
(bythe way, why was size deprecated in favour of sizeof? To my mind,
ismore fitting for a property, i.e. a.size, and sizeof for a function,
sizeof(a). But never mind...) however the code examples on the D
arevery sparse when it comes to typeof(), and some do not actually work
all. I guess I'm a little confused about the purpose of typeof(), as my
logical assumption would be that it would check what type something
and yet the documentation (keyword: "specify") makes me think that
actually more like cast(). Which brings me to two questions: if typeof() is actually like
whyis it there? And how can I detect the type of a variable (or
Sorry if this is a stupid question, but a search of the D newsgroups
aGoogle of the site did not reveal anything to help me.
Jun 30 2004
Walter wrote:typeid returns an instance of class TypeInfo corresponding to the type of typeid's argument. For example: typeid(int) returns an instance of TypeInfo corresponding to int. If two TypeInfo's represent the same type, then their TypeInfo's will compare equal.
Hmmm. This got me thinking: how would one check if one TypeInfo instance represents a base class of another TypeInfo? As far as I can see no helpful information is included in TypeInfo. This could be a huge problem for functions with arbitrary parameter lists, since then all the information you have about the type is the TypeInfo. Hauke
Jun 30 2004
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message news:cbus58$ots$1 digitaldaemon.com...Hmmm. This got me thinking: how would one check if one TypeInfo instance represents a base class of another TypeInfo? As far as I can see no helpful information is included in TypeInfo.
All class objects are instances of TypeClass, which is derived from TypeInfo. In TypeClass, there's a member info giving the .classinfo of the class.This could be a huge problem for functions with arbitrary parameter lists, since then all the information you have about the type is the TypeInfo. Hauke
Jun 30 2004
Walter wrote:typeid returns an instance of class TypeInfo corresponding to the type of typeid's argument. For example: typeid(int) returns an instance of TypeInfo corresponding to int. If two TypeInfo's represent the same type, then their TypeInfo's will compare equal. The reason you must do: typeid(typeof(0)) == typeid(int) instead of: typeof(0) == int is because of parsing ambiguities. Types and expressions follow different grammar rules, and if there wasn't an easy way to distinguish them, it cannot be parsed. For example: foo[] is that a type array of foo's, or is it the array foo?
Speaking of which, how much arm-twisting would it take to get more TypeInfo? I'd like to be able to look at delegates and function pointers and pry their argument count/types apart. (it would be extremely handy for generating language bindings) TypeInfo for other 'metatypes' like associative arrays would be greatly useful too. (lastly, the holy grail: public methods that a given class exposes) -- andy
Jun 30 2004
"Andy Friesen" <andy ikagames.com> wrote in message news:cbuu7u$rpd$1 digitaldaemon.com...Speaking of which, how much arm-twisting would it take to get more TypeInfo? I'd like to be able to look at delegates and function pointers and pry their argument count/types apart. (it would be extremely handy for generating language bindings) TypeInfo for other 'metatypes' like associative arrays would be greatly useful too.
TypeInfo will get better.
Jun 30 2004
I made a vector class with a static size member strange things happened when size was actually a PROPERTY... what should have been a compiler error turned into a runtime error... and other strange havoc resulted when trying to use the size static member. it's a good thing that nightmare is gone I tell ye :-) up with sizeof, down with size Dan Williams wrote:I'm having a little trouble with typeof(). The D documentation says: "Typeof is a way to specify a type based on the type of an expression." That's fair enough, but in that case how do you DETECT the current type of an expression? I was hoping that either typeof would return something to tell me a current type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like that in any case. However, the compiler keeps yelling that it expects a dot, and I'm really not sure how to do what I want. Things like typeof(a).sizeof work fine (by the way, why was size deprecated in favour of sizeof? To my mind, size is more fitting for a property, i.e. a.size, and sizeof for a function, i.e. sizeof(a). But never mind...) however the code examples on the D site are very sparse when it comes to typeof(), and some do not actually work at all. I guess I'm a little confused about the purpose of typeof(), as my initial logical assumption would be that it would check what type something is, and yet the documentation (keyword: "specify") makes me think that it's actually more like cast(). Which brings me to two questions: if typeof() is actually like cast(), why is it there? And how can I detect the type of a variable (or expression)? Sorry if this is a stupid question, but a search of the D newsgroups and a Google of the site did not reveal anything to help me.
Jun 30 2004
On Wed, 30 Jun 2004 14:17:21 -0700, Daniel Horn <hellcatv hotmail.com> wrote:I made a vector class with a static size member strange things happened when size was actually a PROPERTY... what should have been a compiler error turned into a runtime error... and other strange havoc resulted when trying to use the size static member. it's a good thing that nightmare is gone I tell ye :-) up with sizeof, down with size
Hmm... but say you now make a static class member called sizeof.. class A { static int sizeof; } void main() { A a = new A(); printf("%d\n",a.sizeof); } this compiles, runs and prints "0". Remove the "static int sizeof" and it compiles, runs and prints "4". Should this be allowed, should it be a compiler error? I think it should. ReganDan Williams wrote:I'm having a little trouble with typeof(). The D documentation says: "Typeof is a way to specify a type based on the type of an expression." That's fair enough, but in that case how do you DETECT the current type of an expression? I was hoping that either typeof would return something to tell me a current type, or that I could at the very least to typeof comparison checking such as if (typeof('c') == typeof(int)) - something like that in any case. However, the compiler keeps yelling that it expects a dot, and I'm really not sure how to do what I want. Things like typeof(a).sizeof work fine (by the way, why was size deprecated in favour of sizeof? To my mind, size is more fitting for a property, i.e. a.size, and sizeof for a function, i.e. sizeof(a). But never mind...) however the code examples on the D site are very sparse when it comes to typeof(), and some do not actually work at all. I guess I'm a little confused about the purpose of typeof(), as my initial logical assumption would be that it would check what type something is, and yet the documentation (keyword: "specify") makes me think that it's actually more like cast(). Which brings me to two questions: if typeof() is actually like cast(), why is it there? And how can I detect the type of a variable (or expression)? Sorry if this is a stupid question, but a search of the D newsgroups and a Google of the site did not reveal anything to help me.
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004









"Dan Williams" <dnews ithium.NOSPAM.net> 