Advertisement
nponeccop

Untitled

Jul 27th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE TemplateHaskell #-}
  2. {-# LANGUAGE GADTs #-}
  3. module Widget (
  4.   widget
  5. ) where
  6.  
  7. import Control.Lens.TH
  8.  
  9. data Widget = Widget -- the second Widget is value constructor you missed
  10.   { _widgetX :: Int  -- _ is a convention, because _widgetX is useless
  11.   , _widgetY :: Int  -- because of bad design of records
  12.   , _widgetZ :: Int  -- the prefixes are also a convention, mere _x is useless t                                                                                                                oo
  13.   } deriving (Show, Ord, Eq)
  14.  
  15. makeLenses ''Widget
  16.  
  17. class WidgetClass a where
  18.   moveX :: Int -> a -> a -- sort of virtual method declaration
  19.  
  20. instance WidgetClass Widget where
  21.   moveX = undefined -- sort of virtual method definition
  22.  
  23. widget :: Int -> Int -> Int -> Widget
  24. widget x y z | x > 0 && y > 0 && z > 0 = Widget x y z -- smart constructor
  25. widget _ _ _ = error "parameters out of range"
  26.  
  27. data IWidget where
  28.   IWidget :: (Show a, WidgetClass a) => a -> IWidget
  29.  
  30. instance Show IWidget where
  31.   show (IWidget x) = show x
  32.  
  33. data Dummy = Dummy Int deriving Show
  34. instance WidgetClass Dummy where
  35.    moveX = undefined
  36.  
  37. main = print $  [IWidget $ widget 1 1 2, IWidget $ Dummy 42]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement