DEV Community

rkeeves
rkeeves

Posted on

Your "Static" Types Suck: You are checking nothing, Jon Snow

Hi, Mr. Static Typer Tiger!

(be it Java, Rust, C++, C, Go, Haskell or even ugly little duckling TypeScript)

You are the kind of guy who does not play around in Adult Kindergarten:

jspy

You are not a toddler. You fight like a Man:

java

You are a Knight in Statically Typed Armor.

You, with the other honorable Knights, stand guard on top of the Type System to protect the Realm:

snow

You, with your allies - the Knights of the Type Watch - keep a watchful eye over the kingdom.

The goal of this article is:

To build up in us some empathy towards people who use dynamically typed languages.

I want us to feel what dynamically typed people might feel, when they get force-fed our statically typed agenda.

Get the low hanging fruit out of the way

You probably know that in normal type systems, you can pull off crazy nonsense.

We'll be quick and only look at three.

Duck typing - Gary and his Ostrich

The most obvious cheap shot is of course duck typing and all the bonuses and maluses which come with it.

If it looks like a duck, quacks like a duck... it could be still living in pain:

duckts

We don't want to go into subtype vs. assignment, this is enough for us.

Covariance and Contravariance - Smoking Hot

A much more prevalent topic, which is not TypeScript specific, is that they allow a really macho thing: Smoking.

Let's see how smoking will pan out for our statically typed entourage...

zoolander

We can play with Petrol in TypeScript easily even at runtime.

In TypeScript, if we have an array of SprayingPlayers we can add a last one which lights up a cigarette:

zoolanderts

And this works in other languages too!

For example, Java:

zoolanderjava

We don't want to go into covariance vs. contravariance, this is enough for us.

The limits of static analysis - Forgetfulness

You could pickup everyone on Planet Earth... according to Groundhog day, because you'd have infinite amount of tries, and each time you fail, you'd learn about that person more, which you could use in your next encounters.

Unfortunately common type systems, do not play by the rules of Groundhog day.

Common static type systems cannot express evolving properties and invariants.

When you wake up, you will FORGET everything about what happened earlier.

groundhog

Even if you discount type erasure and other mechanisms, type systems in general don't retain all of the information.

This is why we have:

  • Static properties (deducible even by reading the code)
  • Dynamic properties (deducible only by executing the code and possibly making side-effects)

For example, array bound checks must occur always:

access

But there are other languages than Java.

The Department of War, formerly known as the Department of Defense:

dow

... developed a different language ...

In this language - amongst many other things - you can constrain the type of arrays to use a specific type of indices:

lang

(indices are not necessarily numbers, but I used numbers because it is easier to digest that way)

Of course you can index into the array:

good

But if you index out of the array, you can tell it from the constraints:

bad

The bounds check can be done by simply reading the code, aka static analysis.

Another simple example: Increment a number.

We can define the Increment procedure:

procedure

But we can also define a kind of mathematical statement or 'contract' about Increment:

statement

Then we can use the computer to prove that Increment adheres to the 'contract'.

In our case, static analysis reveals an overflow in this simple code:

falsify

"Static" Type System, peon-cast

Rust, Java, C++, TS etc. devs often say they're fighting the type system.

I want to show you what "fight" means.

I'll use Java, but the same thing applies to Rust, C and even things like React.

Sorry, this will be a bit SOLID, but we're gonna cut to the juicy part really quickly, just bear with me.

There will be two Design Patterns before we get to the kickbox.

In Proxy pattern there's a Proxy between the real thing and the caller:

proxy

In Command pattern each action like click or hover is its own Command:

command

There's a Java library called Selenide which does a roundhouse kick.

It is a web browser automation library. You can click or setValue on a DOM element:

selenide

That $(...) gives back a JVM created ghost, which is hooked onto Proxy. The Proxy redirects(routes) all incoming calls to their respective Commands.

For example .setValue() to SetValue Command:

selenide-patterns

The SetValue does not know that its first - and only - argument is a String.

It just accesses into the args array and peon-casts the value:

peoncast

Same thing applies to Rust, C and even things like React.

Example, peon-cast in Angular:

peoncastangular

Or a low-hanging fruit peon-cast in C++:

peoncastcpp

Lads, let's learn a Song!

We are going to re-learn how types and 1 + 1 works, because apparently:

None of us listened in elementary school!

We'll sing along to a song, together lads!

You get bonus points if you keep up the pace with the lyrics!

Let's sing our Pirate Chantey!

No, we don't say "booty" ’less we talkin’ 'bout gold
Enter fullscreen mode Exit fullscreen mode

ts

We don’t look at chests 'less it's treasure holds!
Enter fullscreen mode Exit fullscreen mode

java

With a hat and a feather and a cutlass on our hip
Enter fullscreen mode Exit fullscreen mode

rust

We don't ever say "she" when we’re talking 'bout a ship!
Enter fullscreen mode Exit fullscreen mode

c

We don't say "b***h" and we don't say "w***e"
Enter fullscreen mode Exit fullscreen mode

go

'Cause that language leads to things like body dysmorphiaaaaaaa
Enter fullscreen mode Exit fullscreen mode

haskell

There was a lady with a golden eye
Enter fullscreen mode Exit fullscreen mode

esa

And the doctor said she would die
Enter fullscreen mode Exit fullscreen mode

adaspace

So she emptied her purse to lift the curse
Enter fullscreen mode Exit fullscreen mode

dow

And prayed to stay alive
Enter fullscreen mode Exit fullscreen mode

adadod

She awoke the very next day
Enter fullscreen mode Exit fullscreen mode

ariane

And in her grave she lay
Enter fullscreen mode Exit fullscreen mode

arianeana

But the scariest part of the story from the start...

Is I bet you assumed the doctor was a man
Enter fullscreen mode Exit fullscreen mode

adaborn

Women are doctors too
Enter fullscreen mode Exit fullscreen mode

agda

And for a fraction of the doubloons!
Enter fullscreen mode Exit fullscreen mode

agdanat

We say "Yo, ho!" but we don't say "ho"
Enter fullscreen mode Exit fullscreen mode

agdadisprove

'Cause "ho" is disrespectful, yo
Enter fullscreen mode Exit fullscreen mode

agdaprove

There was a Next.js with hydration da' here and a json that -
Enter fullscreen mode Exit fullscreen mode

jsbad

'Cause it's "Yo, ho!" but we don't say "ho"
Enter fullscreen mode Exit fullscreen mode

agdawhat

'Cause "ho" is disrespectful, yo
Enter fullscreen mode Exit fullscreen mode

ch

Arrr!

agdahello

arrr

Summary

Thank you for your attention, Jon Snow!

Top comments (0)