www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Regarding typedef

reply F. Almeida <francisco.m.almeida gmail.com> writes:
From what I understood, typedef is going to be replaced by a
templated structure and handle type conversion in that way.
This is understandable because 1) the keyword was easily confused
with C++ typedef, whose role was taken in D by alias (which is a
much less misleading keyword for what it does) and 2) as it was,
typedef was of questionable use (not being able to choose which
casts are permitted removed it of any practicality).

What I'm wondering is if it wouldn't have helped to enhance it
rather than simply remove it. Let us consider this hypothetical
syntax:

typedef MyType real : in(float,double,real), out(real);

It would read "MyType is mapped from real. Only a float, a double or
a real may cast to MyType. MyType may only cast to real." This would
allow all specified casts to be statically checked by the compiler.
Of course, one could also apply this syntax to structs or classes,
in which case the compiler could verify for existing opCast()() or
single-argument constructors between types, and reject the
definition otherwise. The shorter version:

typedef MyType real : float, double, real;

would mean that MyType accepts casts from the listed types, but may
not be cast to another type, and the simplest version:

typedef MyType real;

would be have the same meaning as the deprecated typedef statement.

What do you think?
Aug 04 2010
parent reply Trass3r <un known.com> writes:
 From what I understood, typedef is going to be replaced by a
 templated structure and handle type conversion in that way.

 What do you think?

That discussion is kinda at a standstill. Recently I posted a topic titled "library defined typedef" because the current proposed version only supports initialization from the other type and doesn't really deal with casting. Also it would probably need opImplicitCast I think. But nobody answered.
Aug 04 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Trass3r:
 But nobody answered.

Your ideas seem a starting point. Currently in practical programs a struct+alias this is not able to replace my usages of typedef (in past I have written here several posts showing some usage examples). Bye, bearophile
Aug 04 2010