Generating type signatures for dynamic mixins using Sorbet and Tapioca, by Emily Samp
Abstract
Last year, Tapioca became the official tool for generating RBI type signatures in the Sorbet ecosystem. Using Tapioca, developers can quickly generate accurate type signatures for external Ruby gems, allowing them to use Sorbet in their projects even if most gems have not yet added type signatures.
In this talk, I’ll explain how I implemented new functionality in Tapioca to help it generate type signatures for dynamic mixins in Ruby gems. Along the way, we’ll learn about how Tapioca uses information about the Ruby ObjectSpace to generate type signatures, and how this work has impacted the Ruby language as a whole.
Details
Intended Audience
This talk is intended for an audience of mid-to-advanced level Ruby programmers who have a basic understanding of the Ruby object model.
Outcomes
By the end of this talk, the audience will be able to answer the following questions:
- What is Sorbet? What is the Tapioca gem?
- How does Tapioca learn what constants and methods are available in a gem?
- How can we examine Ruby's Object Space to help us implement advanced features in Tapioca?
- How has work on Tapioca shaped the Ruby language?
Outline
- Introduction and background knowledge
- Sorbet is a gradual type checker for Ruby
- Tapioca is a gem that can generate type signatures for methods and constants defined in untyped gems
- The problem
- When I started working on Tapioca, it was good at generating signatures for most gems
- However, there were some missing edge cases
- For example: creating dynamic mixins to constants, like if the gem
bar
calledFoo.prepend(Bar)
whereFoo
is a constant defined in a different gem - How can we correctly generate types for
Foo
here?
- The solution
- Tapioca uses Sorbet to get a list of constants defined in Object Space when a gem is loaded
- Using source locations, we can identify which of these constants are defined by a given gem
- When a constant is defined in a different gem, we can figure out if the gem is loading it in order to add dynamic mixins to it, and what those mixins are
- From there, we make sure Tapioca adds the relevant type signatures to that constant
- Other developments
- This work was used to justify changes to the Ruby language, specifically the addition of the
Class#attached_object
method in Ruby 3.2
- This work was used to justify changes to the Ruby language, specifically the addition of the
- Conclusion
Pitch
I am a software developer who has worked closely on improving Sorbet and its related tooling, including the Tapioca gem. Throughout the course of my work, I have learned about the Ruby object model and gems like Tapioca use information about Ruby Objects to implement valuable functionality, such as automatic type signature generation.
In this talk, I aim to share some of those learnings with the RubyKaigi audience in a way that is deeply technical, but also approachable, by wrapping it in the story of how I implemented new functionality in Tapioca.
I am an experienced speaker who is known for giving talks that have meaningful technical information but are still enjoyable for Rubyists of all experience levels.