Search code examples
typeselmcontract

Elm - open ended types in a contract


Given the pseudo-contract:

condy: Int -> Int -> a -> b
condy n m a b =
  if n == m then a else b

how can one define the above contract correctly such that the types of a and b are equal but can be any type? Effectively the above removes the need for the extra function condyEffect[Type] for each Type:

condyPrime: Int -> Int -> Bool
condy n m =
  n == m

condyEffectInt: Bool -> Int
condyEffectInt bool k l =
  if bool then k else l

Solution

  • Your condy function would look like this:

    condy: Int -> Int -> a -> a -> a
    condy n m a b =
      if n == m then a else b
    

    The portion a -> a -> a in the type signature just says that the 3rd and 4th parameter of condy must be of the same type, and the final a in the type annotation says the return value needs to be the same type as the third and fourth parameters.