O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create a new project working-with-existentials with a simple stack template:
       stack new working-with-existentials simple
  1. Open src/Main.hs. We will be adding our source here.
  2. Enable the existential support, and define the Main module. Also add StandaloneDeriving extension.
        {-# LANGUAGE ExistentialQuantification, StandaloneDeriving #-}

        module Main where
  1. Define an existential type:
        data Display = forall a . Show a => Display a
  1. Create a standalone instance of Show Display:
        deriving instance Show Display 
  1. Create a list of Displays, and, call it in the main function:
 displayList :: [Display]
 displayList = [ Display 10
              , Display ["One","Two","Three"]
              , Display 10.0
              , Display (Just "Something")
              , Display True ] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required