Jump to page: 1 2
Thread overview
[property] type.name
Feb 15, 2004
Andrew Edwards
Feb 16, 2004
Matthew
Feb 17, 2004
Stewart Gordon
Feb 17, 2004
Andrew Edwards
Feb 17, 2004
Chris Sauls
Feb 17, 2004
Andrew Edwards
Feb 18, 2004
Chris Sauls
Feb 17, 2004
Sean Kelly
Feb 17, 2004
Andrew Edwards
Feb 19, 2004
Sean Kelly
Feb 23, 2004
Matthew
Feb 25, 2004
Andrew Edwards
February 15, 2004
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)
    {
      // ...
    }
  }
}
February 16, 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)
>      {
>        // ...
>      }
>    }
> }


February 17, 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.
February 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)
>     {
>       // ...
>     }
>   }
> }

February 17, 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

February 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.
> - 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
February 17, 2004
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
> 
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.

Andrew
February 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:
> 
>> 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
February 18, 2004
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

February 19, 2004
Andrew Edwards wrote:

> Sean Kelly wrote:
> 
>> There'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.

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

« First   ‹ Prev
1 2