If 2 is an overloaded literal, and + is an overloaded operator, we’ll need something more than equality constraints to handle that.
An attractive solution is type classes as in Haskell, in this case Num
:
class Num a where
fromInteger : Integer -> a
(+) : a -> a -> a
Literals like 2
is then taken to mean fromInteger 2
, which has the type Num a => a
— here a
can be any type that adheres to the Num a
type class constraint, i.e. an instance is defined for it. We’d then introduce the following instances:
instance Num Int8 where ...
instance Num Uint where ...
instance Num Float where ...
I’ll cover type class constraints later in the series.