haskell polymorphism and lists

后端 未结 6 1004
温柔的废话
温柔的废话 2021-02-02 13:45

Suppose I have the following:

class Shape a where
    draw a :: a -> IO ()

data Rectangle = Rectangle Int Int

instance Shape Rectangle where
    draw (Recta         


        
6条回答
  •  清歌不尽
    2021-02-02 14:27

    One way to do it would be with vtables:

    data Shape = Shape {
      draw :: IO (),
      etc :: ...
    }
    
    rectangle :: Int -> Int -> Shape
    rectangle len width = Shape {
      draw = ...,
      etc = ...
    }
    
    circle :: Int -> Int -> Shape
    circle center radius = Shape {
      draw = ...,
      etc = ...
    }
    

提交回复
热议问题