Sorbet - Making Ruby Statically Typed
Written by Nikos Vaggalis   
Wednesday, 04 July 2018

Stripe's Sorbet type checker aims to achieve what Typescript has done for JavaScript; inject static typing to a dynamical language.

But let's start with the basics; what's so wrong with dynamically typed systems that statically type ones, full or partial, are so desirable.

Back in early 2010, I Programmer ran my in-depth article series, Type System Demystified (Part 1, Part 2, Part 3) on the big face-off between types systems (static vs dynamic, strong vs weak), detailing both their hindrances as well as their merits. While things have moved on, the principles I outlined in the articles remain, and their applicability to JavaScript has even been referred to in a 2013 thesis "Loading match and drawing information using node.js - A Work Performed at the Company Svenska" done at the Swedish KTH Royal Institute Of Technology.

What it mostly comes down to is that declaring types leads to fewer runtime errors, but then again that's trading productivity for safety; try to parse an XML file in Java instead of in Perl. You see where this is going, but the truth is that the trend is moving towards mixed or gradual type systems, such as those endorsed by Perl 6, or statically typed systems with type inference, such as those of Haskell.

Sorbet is actually split into two type checker counterparts, dynamic and static. In taking inspiration from RDL, another lightweight system for adding types, type checking and contracts to Ruby, it adopts a hybrid approach, enforcing type checking at runtime with type annotations being represented as a DSL, and statically checking the code and method signatures.

 

 

Its difference from Typescript is that in Sorbet there is no compilation phase or transpill and, since Stripe's codebase is huge, Sorbet's type system should had been made compatible with existing code and libraries to allow for gradual adaptation.

Other properties outlined by Dmitry Petrashko, the developer at Stripe  who is involved in the core project, are:

  • is ridiculously fast, 100k lines per second per core, scaling up to 64 cores

  • written in modern C++

  • the entire thing is designed with nice error messages in mind

  • local type inference, no need to declare local variables

  • non-nullable types by default

  • smart control-flow dependent typing

  • union and intersection types

At the moment, Sorbet is being used internally at Stripe, with developers optionally joining in, but the long term plans are to open source it. Much is not yet still known, but for the time being the core devs have prepared an online playground to get a feel of what's in stock.

Visit https://sorbet.run/ for more.

stripsq

More Information

RubyKaigi 2018-A practical type system for Ruby at Stripe

Related Articles

Type Systems Demystified part 1

Type Systems Demystified part 2 - Weak typing

Type Systems Demystified part 3 - Strong typing

Exposing The Most Frequent Mistakes In Programming

Pro DLR in .NET 4.0
IronJS - In Conversation with Fredrik Holmström

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


A Tee Is Not Just For Xmas - Top Tees
20/12/2024

Programmer gifts - easy idea, difficult implementation.  Here's our pick of tee-shirts for giving, buying or just wearing at any time of the year.



Amazon Adds Agents To Q Developer
05/12/2024

Amazon has announced enhancements to Amazon Q Developer, including agents that automate unit testing, documentation generation, code reviews, and a capability to help users "address operational issues [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 04 July 2018 )