Search code examples
purescript

In Purescript; What is the difference between -> and =>?


While working through PureScript tutorials, the code samples start using "=>" without introducing it. As results I don't understand when to use '=>' and not '->'.

For example this uses '=>':

instance showArray :: (Show a) => Show (Array a) where
    show array = "[" <> map show array <> "]"

where as this uses '->':

greet :: forall r. { name :: String | r} -> String
greet namedThing = "Hello, " ++ namedThing.name

Solution

  • (Show a) => is a type constraint, which restricts the type a to instances of the class Show and a -> b is a type of function. So this code

    foo :: forall a. (Show a) => a -> b
    

    is a function foo from a to b and type a must have an instance of class Show

    In OO languages it would be something like this

    public B foo<A,B>(A x) where A:IShow