Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE TemplateHaskell #-}
- {-# LANGUAGE GADTs #-}
- module Widget (
- widget
- ) where
- import Control.Lens.TH
- data Widget = Widget -- the second Widget is value constructor you missed
- { _widgetX :: Int -- _ is a convention, because _widgetX is useless
- , _widgetY :: Int -- because of bad design of records
- , _widgetZ :: Int -- the prefixes are also a convention, mere _x is useless t oo
- } deriving (Show, Ord, Eq)
- makeLenses ''Widget
- class WidgetClass a where
- moveX :: Int -> a -> a -- sort of virtual method declaration
- instance WidgetClass Widget where
- moveX = undefined -- sort of virtual method definition
- widget :: Int -> Int -> Int -> Widget
- widget x y z | x > 0 && y > 0 && z > 0 = Widget x y z -- smart constructor
- widget _ _ _ = error "parameters out of range"
- data IWidget where
- IWidget :: (Show a, WidgetClass a) => a -> IWidget
- instance Show IWidget where
- show (IWidget x) = show x
- data Dummy = Dummy Int deriving Show
- instance WidgetClass Dummy where
- moveX = undefined
- main = print $ [IWidget $ widget 1 1 2, IWidget $ Dummy 42]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement