Example of contravariance
Posted
by
Misha
on Stack Overflow
See other posts from Stack Overflow
or by Misha
Published on 2011-03-18T12:20:59Z
Indexed on
2011/03/19
16:10 UTC
Read the original article
Hit count: 220
scala
|contravariance
I am thinking of the following example to illustrate why contravariance is useful.
Let's consider a GUI framework with Widgets
, Events
, and Event Listeners
.
abstract class Event;
class KeyEvent extends Event
class MouseEvent extends Event
trait EventListener[-Event] { def listen(e:Event) }
Let Widgets
define the following methods:
def addKeyEventListener(listener:EventListener[KeyEvent])
def addMouseEventListener(listener:EventListener[MouseEvent])
These methods accept only "specific" event listeners, which is fine. However I would like to define also "kitchen-sink" listeners, which listen to all events, and pass such listeners to the "add listener" methods above.
For instance, I would like to define LogEventListener
to log all incoming events
class LogEventListener extends EventListener[Event] {
def listen(e:Event) { log(event) }
}
Since the trait EventListener
is contravariant in Event
we can pass LogEventListener
to all those "add listener" methods without losing their type safety.
Does it make sense ?
© Stack Overflow or respective owner