Last active
July 21, 2018 09:31
-
-
Save kuribas/fa0955ee330145fc87c0f57825060c3e to your computer and use it in GitHub Desktop.
haskell inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} | |
| module Subtype where | |
| data Derived super sub = Derived super sub | |
| class IsInstance b a where | |
| getData :: (a -> v) -> b -> v | |
| setData :: (a -> s -> s) -> b -> s -> s | |
| instance IsInstance (Derived a b) a where | |
| getData getter (Derived a _) = getter a | |
| setData setter (Derived a _) = setter a | |
| instance IsInstance b c => IsInstance (Derived a b) c where | |
| getData getter (Derived _ b) = getData getter b | |
| setData setter (Derived _ b) = getData setter b | |
| data Super = Super {name :: String} | |
| data Sub = Sub {age :: Int} | |
| getName :: IsInstance b Super => b -> String | |
| getName = getData name | |
| getAge :: IsInstance b Sub => b -> Int | |
| getAge = getData age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment