Quick actions

cmd+k|ctrl+k

Navigation

Languages

Existential Quantification (Answer)

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2016-08-25T12:25:18Z

Updated

2016-08-25T12:25:18Z

{-# LANGUAGE ExistentialQuantification #-}

-- This Foo can only be constructed with instances of Show as its argument.
data Foo a = forall a. Show a => Foo a

-- Note that there is no "Show a => ..." context here:
-- Foo itself already carries that constraint around with it.
instance Show (Foo a) where
  show (Foo a) = show a


getFoo :: String -> Foo a
getFoo "five" = Foo 5
getFoo "false" = Foo False

main = print . getFoo =<< getLine
INFO