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:

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:

I’ll cover type class constraints later in the series.

--

--

--

MSc Computer Science, working with functional programming in the industry — github.com/ahnfelt

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joakim Ahnfelt-Rønne

Joakim Ahnfelt-Rønne

MSc Computer Science, working with functional programming in the industry — github.com/ahnfelt

More from Medium

Moss Plantu and the Importance of Being Idle

Your Writer Muscle — Get It a Trainer…

Stuff That Hurts

My 5 Day Fasting Regimen, A Promise I Must Keep, and How I’m Going to Do It