我有一个大的打开的消息类型集和处理程序Message -> IO ()
列表,每个处理程序都期望某种类型的消息而忽略所有其他消息。
需要一个函数anymessage -> [Message -> IO ()] -> IO ()
。我可以用type Message = Dynamic
甚至type Message = String
(和show
/read
)来做这个工作。
有更清洁的解决方案吗?
这听起来像通常的可扩展异常/消息处理程序。看到:
- /An Extensible dynamic - typed Hierarchy of Exceptions/, Simon Marlow, 2006.
参见XMonad的Message类:
--
-- User-extensible messages must be a member of this class.
--
class Typeable a => Message a
-- |
-- A wrapped value of some type in the 'Message' class.
--
data SomeMessage = forall a. Message a => SomeMessage a
-- |
-- And now, unwrap a given, unknown 'Message' type, performing a (dynamic)
-- type check on the result.
--
fromMessage :: Message m => SomeMessage -> Maybe m
fromMessage (SomeMessage m) = cast m
-- X Events are valid Messages.
instance Message Event
等等。