Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 30, 2004 typeof headache | ||||
---|---|---|---|---|
| ||||
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. |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan Williams | 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 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. > > |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan Williams | > 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. 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. |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to me | 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 > 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. > > 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. > > |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan Williams | 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.
|
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | > 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. |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | 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 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. > |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bent Rasmussen | 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. > > > |
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan Williams | > 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.
|
June 30, 2004 Re: typeof headache | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | 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 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. > |
Copyright © 1999-2021 by the D Language Foundation