www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to cast type from typeinfo in runtime?

reply badmadevil <badmadevil gmail.com> writes:
Hi, i want to format complex number by Tango's Layout class.
As a sub-problem, I need to convert Arg p to a proper typed variable u.

It can be done if convert p in _all_ 6 cases (like the ifloat case).
But can the type (of p) used in casting be directly derived from argument
_type_ (of Typeinfo)?

Thank for help.
----------
class ComplexLayout : Layout!(char) {
  protected override char[] unknown (char[] output, char[] format, TypeInfo
type, Arg p) {
    char[] s ;
    creal u ;
    // p is of type Arg, alias from void*

    char tc = type.classinfo.name[9] ;
    switch(tc)  {
      case 'q','r','c','j','p': // complex & imagery
        u = cast(creal)( *cast(typeof(type.classinfo.create())*)p ) ;       //
failed try
     // u = cast(creal)( *cast(typeof(Object.factory(type.toString))*)p ) ; //
failed try
        break ;
      case 'o': // ifloat
        u = cast(creal)( *cast(ifloat*)p ) ;
        break ;
      default:
        return "{unhandled argument type: " ~ type.toString ~ '}';
    }

    // format u into s

    return s ;
  }
}
Feb 17 2008
parent reply downs <default_357-line yahoo.de> writes:
I'm not a Tango user, but I don't think so.

This is because typeinfo is a runtime value, but D types are completely
compile-time only.

 --downs
Feb 17 2008
parent badmadevil <badmadevil gmail.com> writes:
downs wrote:
 I'm not a Tango user, but I don't think so.
 
 This is because typeinfo is a runtime value, but D types are completely
compile-time only.
 
  --downs

I see, thank you.
Feb 19 2008