Adding Types to Untyped Languages

Sometimes, even the most dynamic languages need to classify and check data. Now, you can add your own types to any language!

We’ve talked a lot about typed languages lately, like Rust or Idris. These are languages that have a way to classify, or type, the data that’s in them. These types can be checked by the language to prevent errors or even help you write code. What about all those untyped languages? The Pythons, Rubys, and JavasScripts of the world? Are they left out in the cold, no types to cover them in the darkness of winter?

Well, no, actually!

Enterprising programmers have invented methods to add types to untyped languages, and they do it in a few different ways! In this rest of this article, we’ll talk a little bit about some of them and give examples of how types are added to the three languages we named above! Namely, we’ll be talking about Sorbet for Ruby, TypeScript for JavaScript, and pytype for Python.

First, there’s a neat little observation that makes adding types to untyped languages a lot easier: every untyped language can be seen as a typed language with only one type. We can call this lone type Any like TypeScript does, or Untyped like Sorbet does, or any number of other names but the point is the same: if you give everything in the language the type Untyped then everything type checks. After all, you’re always checking that Untyped equals Untyped. That can’t fail!

Once we know we can give every term in an untyped language a dummy type that always passes type checking, this means that we can gradually add in more informative types like “boolean” or “integer” to bits of code. We can start expressing ideas like “the address argument to the web server response function needs to be a string” or “collision code for the hit boxes needs to take only integers” in the code bit by bit. This is, no surprise, called gradual typing. It’s really useful for programmers working in big teams with bigger code bases because gradually adding types works like adding comments to the code that can be checked to see if the code is being used correctly.

Sorbet is a gradual type system for Ruby and TypeScript is basically a gradual type system for JavaScript. The reason why I say “basically” is because, technically speaking, TypeScript is a gradually typed language that compiles to JavaScript but isn’t JavaScript.

Python has a cool project that’s a little different than the gradual typing approach: pytype, which uses type inference. Type inference is related to type checking, but it’s a two-step process: first the type checker tries to figure out the types of everything not explicitly labeled with a type, and then checks them. What does “figure out the types” mean? For example:

aVariable + anotherVar

If you see something like this, assuming that this is Python code, what do you know about the kinds of data in the variables? You know that they have either numbers or strings. If you instead see:

aVariable + str(anotherVar)

Then you know that aVariable must be a string but anotherVar could have literally anything in it. Type inference is this process of looking at how data is used to figure out exactly what it is, but the inference phase of type checking is way more complicated than just the simple rules we thought about here. Once types have been inferred then the typechecker tests to see if the inferences it made combine together to make a sensible program.

This is exactly what pytype does. It looks at your code to figure out what the types are and whether they make sense. You can also add “annotations” of what the types of functions should be to help the type inference algorithm along.

So that’s just a little taste of the ways that the power of typed languages can be combined with the flexibility of untyped languages. If you have a favorite untyped language, try searching for “[your fave language here] gradual typing” or “[your fave language here] optional type system” and see what you can find. There’s just so many cool things language nerds do!

Learn More

Untyped and dynamically typed languages

https://www.tutorialspoint.com/What-are-the-differences-between-untyped-and-dynamically-typed-programming-languages

Sorbet

https://sorbet.org/

type checker for Ruby

https://crypt.codemancers.com/posts/2019-08-12-sorbet-type-checker-for-ruby/

Pytype

https://www.freecodecamp.org/news/how-to-quickly-find-type-issues-in-your-python-code-with-pytype-c022782f61c3/

speedy python checker

https://www.infoworld.com/article/3379122/microsoft-unveils-speedy-type-checker-for-python.html

Typescript

https://en.wikipedia.org/wiki/TypeScript

Typescript: the basics

https://www.codeproject.com/Articles/802722/TypeScript-The-Basics

Into to Typescript

https://medium.com/swlh/intro-to-typescript-63bd15c2cd0e

Author

  • Clarissa Littler

    Clarissa has worked in mathematics, physics, and computer science research but spends much of her time now trying to make computer science education accessible to a broader audience.

Also In The October 2019 Issue

Bring out your virtual carving knives — it’s time to give your digital pumpkins some spooky faces!

30+ ideas for STEAM-theme gifts for kids of all ages!

Teach kids basic coding skills by letting them program Botley to zoom around the room, draw shapes, and even avoid obstacles!

How 3D printing could help us get to Mars, and create new tools, homes, spacecrafts — even organs!

No discussion of design is complete without the history of lorem ipsum. It's more than placeholder text you stuff into a visual design.

"Hello World!" is one of the first programs you learn how to code. Here's the phrase in 4 languages with links to 100 more examples.

Learn the delicious-sounding secrets that websites use to keep your passwords safe from hackers.

A simple, quirky theorem with big applications, from picking socks to counting hairs.

Are you ready to create your virtual own solar system? With a little Python code and a little math, the sky’s the limit!

Learn some of the tricks game developers use to simulate an extra dimension.

How scammers can trick you into downloading malware onto your own computer.

There are pros and cons to networking all the “smart” devices in your home. What surprises does the future hold?

Sometimes, even the most dynamic languages need to classify and check data. Now, you can add your own types to any language!

Is it possible to steal software? And how do we know who owns code?

Check out this nifty feature that helps programs distinguish between variables with different scopes.

Create a simple electronic game with CircuitPython and Adafruit, and test your reflexes against friends and family!

Links from the bottom of all the October 2019 articles, collected in one place for you to print, share, or bookmark.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!