Haskell record syntax and type classes

后端 未结 4 1153
抹茶落季
抹茶落季 2020-12-17 14:57

Suppose that I have two data types Foo and Bar. Foo has fields x and y. Bar has fields x and z. I want to be able to write a function that takes either a Foo or a Bar as

4条回答
  •  再見小時候
    2020-12-17 15:53

    If you make the types instances of Foldable you get a toList function that you can use as the basis of your accessor.

    If Foldable doesn't by you anything, then maybe the right approach is to define the interface you want as a type class and figure out a good way to autogenerate the derived values.

    Perhaps by deriving from doing

    deriving(Data)
    

    you could use gmap combinators to base your access off.

提交回复
热议问题