www.digitalmars.com         C & C++   DMDScript  

D - [property] type.name

reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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
next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
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
prev sibling next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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:

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
parent Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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:
 
 Can we have a .name property for types?

 one possible use:

<snip contrived example>

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.
 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
prev sibling next sibling parent reply Chris Sauls <ibisbasenji yahoo.com> writes:
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
parent reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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.
 - Invironz
 

That 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
parent Chris Sauls <ibisbasenji yahoo.com> writes:
Response below inclusion.

Andrew Edwards wrote:
 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.
 - Invironz

That 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

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. - Invironz
Feb 18 2004
prev sibling next sibling parent reply Sean Kelly <sean ffwd.cx> writes:
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
parent reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
Sean Kelly wrote:
 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

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. Andrew
Feb 17 2004
parent Sean Kelly <sean ffwd.cx> writes:
Andrew Edwards wrote:

 Sean Kelly wrote:
 
 There's a typeof() function, though I agree that this might be a nice 
 default property to add.

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.

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. Sean
Feb 19 2004
prev sibling parent reply "Matthew" <matthew.hat stlsoft.dot.org> writes:
 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
parent Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
Matthew wrote:
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:


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()). Andrew
Feb 24 2004