D - [property] type.name
- Andrew Edwards (49/49) Feb 15 2004 Can we have a .name property for types?
- Matthew (4/53) Feb 15 2004 I thought we already had one.
-
Stewart Gordon
(15/18)
Feb 17 2004
- Andrew Edwards (31/49) Feb 17 2004 Contrived though it may be, I have not claim to know-it-all (or anything...
- Chris Sauls (15/71) Feb 17 2004 I suggested once some time ago that primitive type keywords should be
- Andrew Edwards (5/25) Feb 17 2004 That does not work. The compiler does not allow the use of typeof(foo)
- Chris Sauls (9/37) Feb 18 2004 Of course it isn't, /because/ type keywords are not expressions. If
- Sean Kelly (4/5) Feb 17 2004 There's a typeof() function, though I agree that this might be a nice
- Andrew Edwards (13/25) Feb 17 2004 The type of property does not really work in this sinerio.
- Sean Kelly (13/32) Feb 19 2004 If you mostly care about it being used with built-in types then use
- Matthew (3/52) Feb 22 2004 I think that's a good idea, but you're motivating example is not the bes...
- Andrew Edwards (10/21) Feb 24 2004 [snip]
Can we have a .name property for types? one possible use: ================== import std.stream; int main() { char c; TScan!(char).scan(c); TPrint!(char).echo(c); return 0; } template TScan(T) { // Get information from stdin void scan (out T t, ...) { switch(T.name) { case "char" : std.c.stdio.scanf("%c",&t);break; case "char[]": std.c.stdio.scanf("%.*s", &t);break; case "int" : std.c.stdio.scanf("%d",&t);break; case "double": std.c.stdio.scanf("%f",&t);break; } } // Get information from stream void read(out T t, ...) { switch(T.name) { // ... } } } template TPrint(T) { // Output information to stdout void echo (T t, ...) { switch(T.name) { case "char" : printf("%c",t);break; case "char[]": printf("%.*s",t);break; case "int" : printf("%d",t);break; case "double": printf("%f",t);break; } } // Output information to stream void write (T t, ...) { switch(T.name) { // ... } } }
Feb 15 2004
I thought we already had one. If not, "Doh!" to me, and "hear, hear" to Andrew "Andrew Edwards" <remove_ridimz remove_yahoo.com> wrote in message news:c0om0c$29rb$1 digitaldaemon.com...Can we have a .name property for types? one possible use: ================== import std.stream; int main() { char c; TScan!(char).scan(c); TPrint!(char).echo(c); return 0; } template TScan(T) { // Get information from stdin void scan (out T t, ...) { switch(T.name) { case "char" : std.c.stdio.scanf("%c",&t);break; case "char[]": std.c.stdio.scanf("%.*s", &t);break; case "int" : std.c.stdio.scanf("%d",&t);break; case "double": std.c.stdio.scanf("%f",&t);break; } } // Get information from stream void read(out T t, ...) { switch(T.name) { // ... } } } template TPrint(T) { // Output information to stdout void echo (T t, ...) { switch(T.name) { case "char" : printf("%c",t);break; case "char[]": printf("%.*s",t);break; case "int" : printf("%d",t);break; case "double": printf("%f",t);break; } } // Output information to stream void write (T t, ...) { switch(T.name) { // ... } } }
Feb 15 2004
While it was 2/15/04 8:49 PM throughout the UK, Andrew Edwards sprinkled little black dots on a white screen, and they fell thus:Can we have a .name property for types? one possible use:<snip contrived example> Just looking at it, if you're not going to have anything in common between types, why define a template at all? Or if you are, but it's been omitted from your example, isn't that what template specialisations are for? Even if there is a use, carrying around the whole name of the type seems inefficient to me. Maybe if there's still a will, someone could come up with a nicer way.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Feb 17 2004
Stewart Gordon wrote:While it was 2/15/04 8:49 PM throughout the UK, Andrew Edwards sprinkled little black dots on a white screen, and they fell thus:Contrived though it may be, I have not claim to know-it-all (or anything much about programming for that matter). While I strive to attain that status some day after death, I will settle for thinking outside the box right now. Thanks, I consider it a complement that you even noticed.Can we have a .name property for types? one possible use:<snip contrived example>Just looking at it, if you're not going to have anything in common between types, why define a template at all?The template has nothing to do with the request. I simply posted everything that was on my screen when I tried to use that property. I could have posted: char t; switch(t.name) { case "char" : std.c.stdio.scanf("%c",&t);break; case "char[]": std.c.stdio.scanf("%.*s", &t);break; case "int" : std.c.stdio.scanf("%d",&t);break; case "double": std.c.stdio.scanf("%f",&t);break; } and you would have been none the wiser.Or if you are, but it's been omitted from your example, isn't that what template specialisations are for?Currently my knowledge expand as I experiment with what I've got in front of me: a compiler, a text editor, and not much programming experience. As soon as I learn how to use those feature I'll be sure to apply them more to your liking.Even if there is a use, carrying around the whole name of the type seems inefficient to me. Maybe if there's still a will, someone could come up with a nicer way.... Stewart.My first thought was switch(typeof(t)) { case char: ... } but that didn't work. Not knowing as much as you, I thought that it might not be such a bad idea not to allow this behavior. The only way I could think of achieving the same result was to attach a .name property to types. Andrew
Feb 17 2004
I suggested once some time ago that primitive type keywords should be legal expressions. If they were, this would already be do-able using typeof(), a la: switch (typeof(foo)) { case int: ... case double: ... ... ... ... default: ... } - Chris S. - Invironz Andrew Edwards wrote:Can we have a .name property for types? one possible use: ================== import std.stream; int main() { char c; TScan!(char).scan(c); TPrint!(char).echo(c); return 0; } template TScan(T) { // Get information from stdin void scan (out T t, ...) { switch(T.name) { case "char" : std.c.stdio.scanf("%c",&t);break; case "char[]": std.c.stdio.scanf("%.*s", &t);break; case "int" : std.c.stdio.scanf("%d",&t);break; case "double": std.c.stdio.scanf("%f",&t);break; } } // Get information from stream void read(out T t, ...) { switch(T.name) { // ... } } } template TPrint(T) { // Output information to stdout void echo (T t, ...) { switch(T.name) { case "char" : printf("%c",t);break; case "char[]": printf("%.*s",t);break; case "int" : printf("%d",t);break; case "double": printf("%f",t);break; } } // Output information to stream void write (T t, ...) { switch(T.name) { // ... } } }
Feb 17 2004
Chris Sauls wrote:I suggested once some time ago that primitive type keywords should be legal expressions. If they were, this would already be do-able using typeof(), a la: switch (typeof(foo)) { case int: ... case double: ... ... ... ... default: ... } - Chris S. - InvironzThat does not work. The compiler does not allow the use of typeof(foo) by itself. Not unless you plan to use the type of foo to declare another variable. Even if typeof(foo) was allowed, "case int:" sure isn't. Andrew
Feb 17 2004
Response below inclusion. Andrew Edwards wrote:Chris Sauls wrote:Of course it isn't, /because/ type keywords are not expressions. If they /were/ expressions, as per my suggestion, then this would be perfectly legal. Although, I do admit it would probably be a royal pain for parsing... and that's about the only strong argument I can think of against the idea. - Chris S. - InvironzI suggested once some time ago that primitive type keywords should be legal expressions. If they were, this would already be do-able using typeof(), a la: switch (typeof(foo)) { case int: ... case double: ... ... ... ... default: ... } - Chris S. - InvironzThat does not work. The compiler does not allow the use of typeof(foo) by itself. Not unless you plan to use the type of foo to declare another variable. Even if typeof(foo) was allowed, "case int:" sure isn't. Andrew
Feb 18 2004
Andrew Edwards wrote:Can we have a .name property for types?There's a typeof() function, though I agree that this might be a nice default property to add. Sean
Feb 17 2004
Sean Kelly wrote:Andrew Edwards wrote: >The type of property does not really work in this sinerio. swith(typeof(int)) // not allowed { case int: do something...; break; // not allowed } I would like to get the type of any object passed into the switch and manipulete the object based on it's type. The only real way I can use typeof() right now is to apply a property or use the returned type as a type itself. Not sure how this works with classes or structs but atleast it should work for all builed in types. AndrewCan we have a .name property for types?There's a typeof() function, though I agree that this might be a nice default property to add. Sean
Feb 17 2004
Andrew Edwards wrote:Sean Kelly wrote:If you mostly care about it being used with built-in types then use templates. template typename(T: int) { char[] typename = "int"; } template typename(T: float) { char[] typename = "float"; } Though I agree that some additional reflection features would be nice. SeanThere's a typeof() function, though I agree that this might be a nice default property to add.The type of property does not really work in this sinerio. swith(typeof(int)) // not allowed { case int: do something...; break; // not allowed } I would like to get the type of any object passed into the switch and manipulete the object based on it's type. The only real way I can use typeof() right now is to apply a property or use the returned type as a type itself. Not sure how this works with classes or structs but atleast it should work for all builed in types.
Feb 19 2004
Can we have a .name property for types?I think that's a good idea, but you're motivating example is not the best. Swapping efficient and (type-)safe compile time dispatching for runtime string comparisons is the stuff of Java and .NET. ;)one possible use: ================== import std.stream; int main() { char c; TScan!(char).scan(c); TPrint!(char).echo(c); return 0; } template TScan(T) { // Get information from stdin void scan (out T t, ...) { switch(T.name) { case "char" : std.c.stdio.scanf("%c",&t);break; case "char[]": std.c.stdio.scanf("%.*s", &t);break; case "int" : std.c.stdio.scanf("%d",&t);break; case "double": std.c.stdio.scanf("%f",&t);break; } } // Get information from stream void read(out T t, ...) { switch(T.name) { // ... } } } template TPrint(T) { // Output information to stdout void echo (T t, ...) { switch(T.name) { case "char" : printf("%c",t);break; case "char[]": printf("%.*s",t);break; case "int" : printf("%d",t);break; case "double": printf("%f",t);break; } } // Output information to stream void write (T t, ...) { switch(T.name) { // ... } } }
Feb 22 2004
Matthew wrote:[snip] I'm affraid I'll have to go millitary on that one Matthew: "Private don't know." Hopefully I won't have to hide behind this vail for much longer. In my defense I'd like to say that I was simply trying to learn about templates at the time. The specific example I should have used was the switch. Of course, I requested this feature because I beleave it will facilitate the development of better i/o routines (i.e., scanf()/printf()). AndrewCan we have a .name property for types?I think that's a good idea, but you're motivating example is not the best. Swapping efficient and (type-)safe compile time dispatching for runtime string comparisons is the stuff of Java and .NET. ;)one possible use:
Feb 24 2004