Sorbet

Sorbet

  • Get started
  • Docs
  • Try
  • Community
  • GitHub
  • Blog

›Experimental Features

Getting Started

  • Overview
  • Adopting Sorbet
  • Tracking Adoption
  • Quick Reference
  • Visual Studio Code
  • TypeScript ↔ Sorbet

Static & Runtime

  • Gradual Type Checking
  • Enabling Static Checks
  • Enabling Runtime Checks
  • RBI Files
  • CLI Quickstart
  • CLI Reference
  • Runtime Configuration

Troubleshooting

  • Troubleshooting
  • Why type annotations?
  • FAQ
  • Error Reference
  • Unsupported Ruby Features

Type System

  • sig
  • Type Annotations (non-sig)
  • T.let, T.cast, T.must, T.bind
  • Class Types (Integer, String)
  • Arrays & Hashes
  • Nilable Types (T.nilable)
  • Union Types (T.any)
  • Flow-Sensitivity (is_a?, nil?)
  • T.type_alias
  • Exhaustiveness (T.absurd)
  • T::Struct
  • T::Enum
  • T.untyped
  • Blocks, Procs, & Lambdas
  • Abstract Classes & Interfaces
  • Final Methods & Classes
  • Override Checking
  • Sealed Classes
  • T.class_of
  • T.self_type
  • T.noreturn
  • T.anything
  • T.attached_class
  • Intersection Types (T.all)
  • Generics
  • T::NonForcingConstants
  • Banning untyped

Editor Features

  • Language Server (LSP)
  • Server Status
  • LSP & Typed Level
  • Go to Definition
  • Hover
  • Autocompletion
  • Find All References
  • Code Actions
  • Outline & Document Symbols
  • Documentation Comments
  • Suggesting sigs
  • Highlighting untyped
  • sorbet: URIs

Experimental Features

  • Tuples
  • Shapes
  • Overloads
  • Requiring Ancestors
  • RBS Comments
Edit

Tuples

TODO: This page is still a fragment. Contributions welcome!

[Type1, Type2, ...]

This creates a fixed array type (also referred to as a tuple), which is a fixed-length array with known types for each element. For example, [String, T.nilable(Float)] validates that an object is an array of exactly length 2, with the first item being a String and the second item being a Float or nil.

Warning: Tuples have many known limitations, and should be considered an experimental feature. They may not work as expected or change without notice.

##
# Tuple types work for some simple cases,
# but have many known limitations.
#

extend T::Sig

sig {params(x: [Integer, String]).returns(Integer)}
def foo(x)
  T.reveal_type(x[0]) # Revealed type: `Integer`
end

# --- What you expect ---
foo([0, '']) # ok
foo(['', 0]) # error: type mismatch
foo([]) # error: not right tuple type

# --- What you might not expect ---
foo([0, '', nil]) # ok

# --- Mutation: the ugly ---
y = [0, '']
y[0] = '' # ok (!)
T.reveal_type(y[0]) # Reveal type: `Integer(0)` (!!)

# --- Flow-sensitivity: even uglier ---
y_0 = y[0]
if y_0.is_a?(String)
  puts y_0 # error: This code is unreachable (!!!)
end

→ View on sorbet.run

sorbet internals note: the underlying of a tuple is an Array of the union of each element type.

← sorbet: URIsShapes →

Get started · Docs · Try · Community · Blog · Twitter