How can I write a function which returns one type sometimes and another type other times?

The key is to use Either. For example, suppose you want to write a function that returns the square of a number if the number is positive, but otherwise an error message text. Do this:

{-# LANGUAGE OverloadedStrings #-}

import Data.Text (Text)

squareWithFailure :: Int -> Either Text Int 
squareWithFailure i 
    | i >= 0 = Right (i ^ 2)
    | otherwise = Left "squareWithFailure only takes non-negative input"

Last update: January 12, 2023
Created: January 7, 2023

Comments