library defined typedef
Trass3r
un at known.com
Sun Jul 25 15:14:00 PDT 2010
Probably it's no bad idea to repeat these explanations from Andrei so they
don't get lost in the huge original thread:
1. Something that's just like another type yet "parallel" with it. This is
good for abstractions that encode different units of measurement that
aren't supposed to be mixed.
ParallelTypedef!double Miles;
Such a type should accept explicit initialization from a regular double:
auto dist = Miles(3.2);
However it shouldn't accept initialization from another parallel typedef:
ParallelTypedef!double Kms;
auto dist1 = Kms(4);
auto dist2 = Miles(dist1); // no
Arithmetic operations should only work within Miles but not mixing Miles
with other types. Here's where things already get complicated - you do
want to allow some operations between Miles and double (e.g. "*"), in some
others you don't (e.g. "+"). Here's where a library facility would help:
ParallelTypdef!(double, "allow_arithmetic", "allow_mixed:*,/,%")
Miles;
2. Opaque "handle" types that can be used with overloading. The base type
of the typedef is just the storage strategy:
OpaqueTypedef!int FileHandle;
Such a typedef supports no arithmetic and no implicit conversions. You can
explicitly initialize it from an int and you can cast it back to it using
an explicit cast.
3. Proper subtype. Create a true subtype of a type that allows explicit
initialization from the type and implicit conversion to the type.
SubtypeTypedef!Exception MyException;
4. Proper supertype. The base type implicitly converts to the introduced
type, but not vice versa.
SupertypeTypedef!uint Bits;
More information about the Digitalmars-d
mailing list