DEV Community

Pragmatic types: what are types?

stereobooster on August 22, 2018

The idea of this post is to give you a framework to reason about types (in programming), I'm not gonna try to give an exhaustive and full mathemati...
Collapse
 
jvanbruegge profile image
Jan van Brügge

Not sure why you avoided the term set, it's exactly what a type is, the set of all possible values. For example a union type in Typescript can be expressed as:

type A = number | string;
//same as
number = { n | n ∈ ℝ }
string = { s | s ∈ ∑* }
A = number ∪ string

Systems like LiquidHaskell even allow you to specify a subset of types for your functions, so you can e.g. only pass lists that are proven to not be empty to a function that extracts the first element.

Collapse
 
stereobooster profile image
stereobooster

The issue here is that sets have formal definition and if you say that types are sets you claim that types follow all formal rules for sets. I am not sure that this is the case, even that "simple" types are very similar to sets. I will need to read more to explain exact difference

Collapse
 
polentino911 profile image
Diego Casella

That's why I heartfelt aversion for any dynamic-typed programming language:
why would you throw away such a beautiful feature that does tell you, at compile time, if "X can be fitted into Y / passed to function Z()" (not just checking for undefined), rather than have errors at runtime ? :confused:

Lack of type checking looks great at first. But in the long run...

Collapse
 
emad__elsaid profile image
Emad Elsaid

Finally posted it :D, I like your writing style, keep it coming man