How to model mixins / multiple interfaces in Haskell?

后端 未结 5 1755
闹比i
闹比i 2021-02-08 11:31

I came across this question on modeling inheritance in Haskell and it reminded me that I have a little more complicated version of the same problem. I\'ll adopt the example from

5条回答
  •  不要未来只要你来
    2021-02-08 11:49

    On further reflection, I suppose this is basically a job for extensible records assuming permutativity. As far as I can tell, you'd just have to work with values of the form (r, a), where r is a record containing all the mixed-in data, and a is the original value you wanted. Pairs are already a Functor over the second argument, so you can fmap all your existing functions. For the mixins you could define things like

    pos :: (r <: {_pos :: Vec3}) => (r, a) -> Vec3
    pos (r, a) = r._pos
    

    and so on. Then a coloured physical camera would just be a value of type (r, Camera) where r <: {_pos :: Vec3, _vel :: Vec3, _colour :: Colour}.

    It's too bad all this doesn't exist in standard Haskell yet. Oh well, time for me to go check out some of the extensible records libraries.

提交回复
热议问题