Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Last active October 20, 2016 19:33
Show Gist options
  • Save chadaustin/74786d6ca3c34bba8b33af381606b207 to your computer and use it in GitHub Desktop.
Save chadaustin/74786d6ca3c34bba8b33af381606b207 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeFamilies, FlexibleContexts, ExistentialQuantification, ScopedTypeVariables #-}
type Token = String
class Observable ob where
type EventType ob
addObserver :: ob -> (EventType ob -> IO ()) -> IO Token
removeObserver :: ob -> Token -> IO ()
class Observable ob => ObservableData ob where
readData :: ob -> IO (EventType ob)
data AnyObservableData t = forall o. (ObservableData o, EventType o ~ t) => AnyObservableData o
instance Observable (AnyObservableData t) where
type EventType (AnyObservableData t) = t
addObserver (AnyObservableData o) = addObserver o
removeObserver (AnyObservableData o) = removeObserver o
instance ObservableData (AnyObservableData t) where
readData (AnyObservableData o) = readData o
test :: (ObservableData ob, Show (EventType ob)) => ob -> IO ()
test ob = do
d <- readData ob
putStrLn $ show d
main = do
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment