Keep your Types Small

Why?

(the size of a type is how many values (terms) it can hold (inhabit it).

The less terms can inhabit a running instance of your codebase; the less bugs and surprise you are likely to find.

Why?

enum MarkerColor:
  case Red
  case Green
  case Blue

has fewer surprises than

type MarkerColor = Int

Why?

The State Space of your codebase relates to Entropy and is the biodiversity index of bugs living in it.

 

But there is a surprising impact on a couple of small things you can do...

https://medium.com/swlh/type-system-saving-55db2ca062ff

Recap

(the size of a type is how many values (terms) it can hold (inhabit it).

• Nothing has 0 terms
• Unit has 1 term
• Boolean has 2
• Byte has 256

What's the size of a type?

Recap

You can combine different types os the number of terms they can hold can be:

  • added (enums, Either)

     
  • multiplied (tuples, case classes)
     
  • exponentiated (maps, functions)

     
  • and more...
     

What's the size of a type?

enum Three:
  case One
  case Two(b: Boolean)
case class Four(two: Boolean, too: Boolean)
type Pow[Base, Exponent] = Map[Exponent, Base]
type Eight = Pow[Two, Three]

https://alexknvl.com/posts/counting-type-inhabitants.html

Actions:

Start with your domain model.

Actions:

Make your functions generic.

Actions:

Identify algebras in your code.

Connections

References

  • https://medium.com/swlh/type-system-saving-55db2ca062ff

  • https://alexknvl.com/posts/counting-type-inhabitants.html

  • https://github.com/cohomolo-gy/Type-Arithmetic-and-the-Yoneda-Perspective

  • https://bartoszmilewski.com/2014/09/22/parametricity-money-for-nothing-and-theorems-for-free/

  • https://github.com/Constructive-Programming/scala-cardinality
Made with Slides.com