Home > Mobile >  What is the Haskell equivalence of Rust's associated types?
What is the Haskell equivalence of Rust's associated types?

Time:11-04

What is the Haskell equivalence of Rust's associated types?

CodePudding user response:

Haskell has associated type families. The Contains example on the linked page can be written in Haskell like this:

{-# LANGUAGE TypeFamilies #-}

class Contains a where
  type A a
  type B a
  contains :: a -> A a -> B a -> Bool

data Container = MkContainer Int Int

instance Contains Container where
  type A Container = Int
  type B Container = Int
  contains (MkContainer x y) a b = x == a && y == b
  • Related