Re: Make a DSL serializable
Daniel Trstenjak <daniel.trstenjak <at> gmail.com>
2013-03-24 17:25:43 GMT
> I have a DSL like this:
> data Exp where
> OnEvent :: EventName -> (Int -> Exp) -> Exp
> The "OnEvent" element carries a function (the handler to be called when the
> event happens), and that makes my DSL non showable/serializable.
> How could I fix that? This is a real handicap not to be able to serialize
> the state of my whole application because of that :)
You could have a unique id for your handlers, which might be
an Int or a String and have some kind of registration for the
data Exp where
OnEvent :: EventName -> HandlerId -> Exp
type HandlerId = String
type Handler = (Int -> Exp)
type Handlers = HashMap HandlerId Handler
registerHandler :: Handlers -> (HandlerId, Handler) -> Handlers
getHandler :: Handlers -> HandlerId -> Maybe Handler
But you have to ensure, that for each application run the same
HandlerId also gets the same Handler.
Less flexible but more secure is an ADT for you Handler.
data Handler = DoThat
You can than just pattern match on your handler and don't need any kind
But you can go further and define your own little Handler DSL.