Improve a simple event handler

JN 666total at wp.pl
Sat Jan 15 23:15:16 UTC 2022


I am writing a simple event handler object for observer pattern.

https://gist.github.com/run-dlang/d58d084752a1f65148b33c796535a4e2

(note: the final implementation will use an array of listeners, 
but I want to keep it simple for now and have only one handler 
per event).

Is there some way I could improve this with some D features? My 
main gripes with it are:

1) generic typing. I wish event handler methods such as onResize 
and onWindowTitle could take concrete types like WindowResize and 
WindowTitle rather than the generic Event struct.

2) same when emitting the event. It feels a bit verbose.

I don't want to use classes or polymorphism. I was thinking 
perhaps std.variant could be useful here, although I am not 
exactly sure how it would work in practice.


More information about the Digitalmars-d-learn mailing list