-
Notifications
You must be signed in to change notification settings - Fork 16
The Types namespace
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
.
The top-level, abstract class for a generalised type. All of the following types inherit from this. Do not instantiate.
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
A type which can be either the wrapped type, or nil.
Types::Nilable.new('Integer') # => an integer, or nil
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
The explicit lack of a type. Different type systems may handle this differently.
Types::Untyped.new # => a value with no particular type
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
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
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
A type which is (at least) one of the wrapped types.
Types::Union.new(['String', 'Integer']) # => a string or an integer
A type which matches all of the wrapped types.
Types::Intersection.new(['Enumerable', 'Numeric']) # => a type which is both enumerable and numeric