July 15, 2006
Hi there. I have 2 classes and two typedefs like so:

file events.d:
********************
class BEvent {
..
public uint Time() { return time; }
}

class BMouseEvent: BEvent {
..
public int X() { return x; }
public int Y() { return y; }
}

typedef BMouseEvent BMousePressedEvent;
typedef BMouseEvent BMouseReleasedEvent;
***************************

and in my main method I have this:

main.d
**********
..
bool handleEvent(BEvent e) {
BMousePressedEvent ev = cast(BMousePressedEvent) e;

//BMouseEvent ev = cast(BMouseEvent) e; <-- this works
writefln("Event handler called, on ", ev.X, ".", ev.Y, " at ",
ev.Time);
return true;
}
***************

When I cast the reference to handleEvent to the typedef BMousePressedEvent I get the compiler error:

main.d(99): this for X needs to be type BMouseEvent not type
BMousePressedEvent
main.d(99): this for Y needs to be type BMouseEvent not type
BMousePressedEvent
main.d(99): this for Time needs to be type BEvent not type
BMousePressedEvent

The docs say that typedefs can be implicitly converted to their underlying type, but that doesn't seem to happen. Casting to the underlying type manually(like the comment does) makes it work. Any explanation? Am I doing it wrong, or is this a bug?

/Anders



July 16, 2006
anders@runesson.info wrote:
> Hi there. I have 2 classes and two typedefs like so:
> 
> file events.d: ********************
> class BEvent {
> ...
> public uint Time() { return time; }
> }
> 
> class BMouseEvent: BEvent {
> ...
> public int X() { return x; }
> public int Y() { return y; }
> }
> 
> typedef BMouseEvent BMousePressedEvent;
> typedef BMouseEvent BMouseReleasedEvent;
> ***************************
> 
> and in my main method I have this:
> 
> main.d
> **********
> ...
> bool handleEvent(BEvent e) {
> BMousePressedEvent ev = cast(BMousePressedEvent) e;
> 
> //BMouseEvent ev = cast(BMouseEvent) e; <-- this works
> writefln("Event handler called, on ", ev.X, ".", ev.Y, " at ",
> ev.Time);                  return true;
> }
> ***************
> 
> When I cast the reference to handleEvent to the typedef
> BMousePressedEvent I get the compiler error:
> 
> main.d(99): this for X needs to be type BMouseEvent not type
> BMousePressedEvent
> main.d(99): this for Y needs to be type BMouseEvent not type
> BMousePressedEvent
> main.d(99): this for Time needs to be type BEvent not type
> BMousePressedEvent
> 
> The docs say that typedefs can be implicitly converted to their
> underlying type, but that doesn't seem to happen. Casting to the
> underlying type manually(like the comment does) makes it work.
> Any explanation? Am I doing it wrong, or is this a bug?
> 
> /Anders
> 
> 
> 

Yes, it does seem to be a bug. But this notion of "strong-type" typedefs of classes is new grounds to me (and likely to many people, are many languages which support both notions of OO and strong typedefs?), so it is not certain yet.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D