Skip to content

The Types namespace

Aaron Christiansen edited this page Aug 4, 2020 · 2 revisions

Overview

The Parlour::Types namespace contains the classes you'll instantiate to create generalised types, which can be shared across RBI and RBS generators when converting between those formats.

Note: Where a type is expected as a parameter to any of these, passing a String is valid. This string will be converted to a Types::Raw instance, and should be used for basic types like String or SomeClassYouHaveDefined.

Available Types

Basic Types

Type

The top-level, abstract class for a generalised type. All of the following types inherit from this. Do not instantiate.

Raw

A basic type as a raw string. Technically this string can be anything, but to keep generalised types working across type systems, it should only be a valid Ruby identifier.

Types::Raw.new('Integer') # => an integer

Nilable

A type which can be either the wrapped type, or nil.

Types::Nilable.new('Integer') # => an integer, or nil

Boolean

Generic type for a boolean. Ruby doesn't have one built-in, and each type system has chosen a different way to express it.

Types::Boolean.new # => a boolean, either true or false

Untyped

The explicit lack of a type. Different type systems may handle this differently.

Types::Untyped.new # => a value with no particular type

Collection and Generic Types

Tuple

A fixed-length array of items, each with a known type.

Types::Tuple.new(['String', 'Integer']) # => a 2-element array, whose first item is a string and second item is an integer

Array/Set/Range/Enumerable/Enumerator/Hash

Generic types for Ruby's standard collections.

Types::Array.new('String') # => an array of strings
Types::Hash.new('String', 'Integer') # => a hash of strings to integers

Proc

A type which can be called as a function with #call/#.. Takes parameters as Proc::Parameter instances.

Types::Proc.new([
  Types::Proc::Parameter.new('x', 'String')
], 'Integer') # => a function which takes a string and returns an integer

Compound Types

Union

A type which is (at least) one of the wrapped types.

Types::Union.new(['String', 'Integer']) # => a string or an integer

Intersection

A type which matches all of the wrapped types.

Types::Intersection.new(['Enumerable', 'Numeric']) # => a type which is both enumerable and numeric