Skip to content

Instantly share code, notes, and snippets.

@klapaucius
Created August 16, 2012 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klapaucius/3367586 to your computer and use it in GitHub Desktop.
Save klapaucius/3367586 to your computer and use it in GitHub Desktop.
runtime/compile-time
Prelude> class Pet a where kill :: a -> String
Prelude> data Dog = Dog deriving Show
Prelude> data Cat = Cat deriving Show
Prelude> instance Pet Dog where kill = const "dead"
Prelude> instance Pet Cat where kill = const "c_1|dead> + c_2|alive>"
Prelude> map kill [Cat, Cat] -- диспетчеризация времени компиляции
["c_1|dead> + c_2|alive>","c_1|dead> + c_2|alive>"]
Prelude> map kill [Dog, Dog] -- диспетчеризация времени компиляции
["dead","dead"]
Prelude> data PetBox where Box :: Pet a => a -> PetBox
Prelude> map (\(Box pet) -> kill pet) [Box Dog, Box Cat] -- динамическая диспетчеризация
["dead","c_1|dead> + c_2|alive>"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment