Ruby 3 Sorbet

Ruby 3 Will Introduce Types

April 19, 2019 Posted by News 0 thoughts on “Ruby 3 Will Introduce Types”

 

Though Yukihiro Matsumoto, or “Matz,” has long been opposed to introducing types to Ruby, Ruby 3 will introduce types. The announcement was made in the Ruby Kaigi 2019 Press Conference. There, Stripe engineers Paul Tarjan and Jake Zimmerman demonstrated a type checker they created for Ruby. The type checker is called Sorbet and Stripe was able to successfully adopt it into their existing code base constituting millions of lines of code. What solidifies the announcement of types systems is the fact that Sorbet will be supported by Ruby from the ground up; Ruby 3 will standardize types in its stdlib source.

Here’s example code that you can find on Sorbe.run

 

# typed: true
class A
 extend T::Sig
sig {params(x: Integer).returns(String)}
  def bar(x)
    x.to_s
  end
end
def main
  A.new.barr(91) # error: Typo!
  A.new.bar(“91”) # error: Type mismatch!
end

 

The project to typify Ruby has been years in the making. In 2015, Matz first announced Ruby 3×3, the goal being to make Ruby 3 become 3 times faster than Ruby 2. In an interview a year later, Matz said:

 In the design of the Ruby language we have been primarily focused on productivity and the joy of programming. As a result, Ruby was too slow, because we focused on run-time efficiency, so we’ve tried to do many things to make Ruby faster. For example the engine in Ruby 1.8 was very slow, it was written by me. Then Koichi came in and we replaced the virtual machine. The new virtual machine runs many times faster. Ruby and the Ruby community have continued to grow, and some people still complain about the performance. So we are trying to do new things to boost the performance of the virtual machine. Even though we are an open source project and not a business, I felt it was important for us to set some kind of goal, so I named it Ruby 3×3. The goal is to make Ruby 3 run three times faster as compared to Ruby 2.0. Other languages, for example Java, use the JIT technique, just in time compilation; we don’t use that yet in Ruby. So by using that kind of technology and with some other improvements, I think we can accomplish the three times boost.

In order to boost Ruby’s speed, Matz also tossed around optimizing caching methods and implementing currency into the core Ruby language.

What he didn’t mention in that interview was a type system. The inclusion of the type system was perhaps an inevitable concession Matz was going to have to make. Part of Ruby’s philosophy is having multiple ways to do things so as not to restrict the user. However, this philosophy causes complications when apps built with Ruby grow in size. Properly documenting the code becomes difficult and the inherit slowness of dynamic typing hampers speed. In order to achieve the 3×3 goal, typing was probably needed.

 

 

 

 

Please follow and like us:
0
Tags: