Suppose that you have four types, equivalent to, say, float.
Call one of them Horiz, one Vertic, one Radians, and one Radius.
These are all floats, but when you specify, say,
float dist (Horiz x, Vert y)
{  return sqrt(x * x + y * y);  }
It's important that the arguments aren't Radius and Radians. Or Horiz and Horiz.

Denis Koroskin wrote:
On Wed, 28 Jan 2009 08:45:12 +0300, Daniel Keep <daniel.keep.li...@gmail.com> wrote:


Honestly, I can't see what you're trying to accomplish.  It looks like
you want something that's not called int, but which works exactly like
an int does, and can be passed as one.

If you just want another name for "int", you can use an alias.

From the compiler's POV, there's no difference between "BlockNum" and "int".

alias int BlockNum;
BlockNum a = 42;
someFuncThatTakesAnInt(a);

If you want to have a type that is an int, but which won't allow itself
to directly interact with ints, use a typedef.

Personally, I like this usage for simple numeric values which I don't
want to accidentally mix with other types.  Yes, it's a bit of a pain to
do arithmetic, but that's the trade-off you make.

From the compiler's POV, "BlockNum" and "int" are totally distinct,
incompatible types that just happen to be the same under the hood.

typedef int BlockNum;
BlockNum a = cast(BlockNum) 42;
someFuncThatTakesAnInt(cast(int) a);

The last is if you need something that's basically an int, but you want
it to behave differently.  In that case, a struct with operators is your
best bet.

Let's say you wanted to do something like a Meters struct to store
lengths.  I'd do something like this:

struct Meters {
    private int value;
    int asInt() { return value; }
    int asInt(int v) { return value=v; }
    // ... operator overloads ...
}

Meters a; a.asInt = 42;
someFuncThatTakesAnInt( a.asInt );

I can't really offer more than that, since I don't know what it is
you're trying to accomplish.

  -- Daniel

Perhaps, he wants opImplicitCast?

Reply via email to