module T

Namespaces without any implementation

_____

|_ _| _ _ __ _ _

| || | | | '_ \ / _ \/ __|
| || |_| | |_) |  __/\__ \
|_| \__, | .__/ \___||___/
    |___/|_|

Docs at sorbet.org/docs/sigs

Types that you can pass to `sig`:

- a Ruby class

- [<Type>, <Type>, ...] -- to specify a "tuple"; a fixed-size array with known types for each member

- {key: <Type>, key2: <Type>, ...} -- to speicfy a "shape"; a fixed-size hash
with known keys and type values

- Any of the `T.foo` methods below

typed: strict

Work around for sorbet-runtime wrapped methods.

When a sig is defined, sorbet-runtime will replace the sigged method with a wrapper. Those wrapper methods look like `foo(*args, &blk)` so that wrappers can handle and pass on all the arguments supplied.

However, that creates a problem with runtime reflection on the methods, since when a sigged method is introspected, it will always return its `arity` as `-1`, its `parameters` as `[[:rest, :args], [:block, :blk]]`, and its `source_location` as `[<some_file_in_sorbet>, <some_line_number>]`.

This might be a problem for some applications that rely on getting the correct information from these methods.

This compatibility module, when prepended to the `Method` class, would fix the return values of `arity`, `parameters` and `source_location`.

@example

require 'sorbet-runtime'
::Method.prepend(T::CompatibilityPatches::MethodExtensions)

Constants

Boolean

T::Boolean is a type alias helper for the common `T.any(TrueClass, FalseClass)`. Defined separately from _types.rb because it has a dependency on T::Types::Union.

Public Class Methods

absurd(value) click to toggle source

A way to ask Sorbet to prove that a certain branch of control flow never happens. Commonly used to assert that a case or if statement exhausts all possible cases.

# File lib/types/_types.rb, line 231
def self.absurd(value)
  msg = "Control flow reached T.absurd."

  case value
  when Kernel
    msg += " Got value: #{value}"
  end

  begin
    raise TypeError.new(msg)
  rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
    T::Configuration.inline_type_error_handler(e)
  end
end
all(type_a, type_b, *types) click to toggle source

T.all(<Type>, <Type>, …) – matches an object that has all of the types listed

# File lib/types/_types.rb, line 51
def self.all(type_a, type_b, *types)
  T::Types::Intersection.new([type_a, type_b] + types)
end
any(type_a, type_b, *types) click to toggle source

T.any(<Type>, <Type>, …) – matches any of the types listed

# File lib/types/_types.rb, line 27
def self.any(type_a, type_b, *types)
  type_a = T::Utils.coerce(type_a)
  type_b = T::Utils.coerce(type_b)
  types = types.map {|t| T::Utils.coerce(t)} if !types.empty?
  T::Types::Union::Private::Pool.union_of_types(type_a, type_b, types)
end
assert_type!(value, type, checked: true) click to toggle source

Tells the typechecker to ensure that `value` is of type `type` (if not, the typechecker will fail). Use this for debugging typechecking errors, or to ensure that type information is statically known and being checked appropriately. If `checked` is true, raises an exception at runtime if the value doesn't match the type.

# File lib/types/_types.rb, line 171
def self.assert_type!(value, type, checked: true)
  return value unless checked

  Private::Casts.cast(value, type, cast_method: "T.assert_type!")
end
attached_class() click to toggle source

Matches the instance type in a singleton-class context

# File lib/types/_types.rb, line 71
def self.attached_class
  T::Types::AttachedClassType::Private::INSTANCE
end
bind(value, type, checked: true) click to toggle source

Tells the type checker to treat `self` in the current block as `type`. Useful for blocks that are captured and executed later with instance_exec. Use like:

seconds = lambda do
  T.bind(self, NewBinding)
  ...
end

`T.bind` behaves like `T.cast` in that it is assumed to be true statically.

If `checked` is true, raises an exception at runtime if the value doesn't match the type (this is the default).

# File lib/types/_types.rb, line 161
def self.bind(value, type, checked: true)
  return value unless checked

  Private::Casts.cast(value, type, cast_method: "T.bind")
end
cast(value, type, checked: true) click to toggle source

Tells the typechecker that `value` is of type `type`. Use this to get additional checking after an expression that the typechecker is unable to analyze. If `checked` is true, raises an exception at runtime if the value doesn't match the type.

Compared to `T.let`, `T.cast` is trusted by static system.

# File lib/types/_types.rb, line 127
def self.cast(value, type, checked: true)
  return value unless checked

  Private::Casts.cast(value, type, cast_method: "T.cast")
end
class_of(klass) click to toggle source

Matches any class that subclasses or includes the provided class or module

# File lib/types/_types.rb, line 77
def self.class_of(klass)
  T::Types::ClassOf.new(klass)
end
enum(values) click to toggle source

Matches any of the listed values

# File lib/types/_types.rb, line 56
def self.enum(values)
  T::Types::Enum.new(values)
end
let(value, type, checked: true) click to toggle source

Tells the typechecker to declare a variable of type `type`. Use like:

seconds = T.let(0.0, Float)

Compared to `T.cast`, `T.let` is checked by static system.

If `checked` is true, raises an exception at runtime if the value doesn't match the type.

# File lib/types/_types.rb, line 142
def self.let(value, type, checked: true)
  return value unless checked

  Private::Casts.cast(value, type, cast_method: "T.let")
end
must(arg) click to toggle source

A convenience method to `raise` when the argument is `nil` and return it otherwise.

Intended to be used as:

needs_foo(T.must(maybe_gives_foo))

Equivalent to:

foo = maybe_gives_foo
raise "nil" if foo.nil?
needs_foo(foo)

Intended to be used to promise sorbet that a given nilable value happens to contain a non-nil value at this point.

sig {params(arg: T.nilable(A)).returns(A)}

# File lib/types/_types.rb, line 210
def self.must(arg)
  return arg if arg
  return arg if arg == false

  begin
    raise TypeError.new("Passed `nil` into T.must")
  rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
    T::Configuration.inline_type_error_handler(e)
  end
end
nilable(type) click to toggle source

Shorthand for T.any(type, NilClass)

# File lib/types/_types.rb, line 35
def self.nilable(type)
  T::Types::Union::Private::Pool.union_of_types(T::Utils.coerce(type), T::Utils::Nilable::NIL_TYPE)
end
noreturn() click to toggle source

Indicates a function never returns (e.g. “Kernel#raise”)

# File lib/types/_types.rb, line 46
def self.noreturn
  T::Types::NoReturn::Private::INSTANCE
end
proc() click to toggle source

Creates a proc type

# File lib/types/_types.rb, line 61
def self.proc
  T::Private::Methods.start_proc
end
reveal_type(value) click to toggle source

A way to ask Sorbet to show what type it thinks an expression has. This can be useful for debugging and checking assumptions. In the runtime, merely returns the value passed in.

# File lib/types/_types.rb, line 224
def self.reveal_type(value)
  value
end
self_type() click to toggle source

Matches `self`:

# File lib/types/_types.rb, line 66
def self.self_type
  T::Types::SelfType::Private::INSTANCE
end
type_alias(type=nil, &blk) click to toggle source

Constructs a type alias. Used to create a short name for a larger type. In Ruby this returns a wrapper that contains a proc that is evaluated to get the underlying type. This syntax however is needed for support by the static checker. Example usage:

NilableString = T.type_alias {T.nilable(String)}

sig {params(arg: NilableString, default: String).returns(String)}
def or_else(arg, default)
  arg || default
end

The name of the type alias is not preserved; Error messages will be printed with reference to the underlying type.

TODO Remove `type` parameter. This was left in to make life easier while migrating.

# File lib/types/_types.rb, line 98
def self.type_alias(type=nil, &blk)
  if blk
    T::Private::Types::TypeAlias.new(blk)
  else
    T::Utils.coerce(type)
  end
end
type_parameter(name) click to toggle source

References a type parameter which was previously defined with `type_parameters`.

This is used for generic methods. Example usage:

sig
.type_parameters(:U)
.params(
  blk: T.proc.params(arg0: Elem).returns(T.type_parameter(:U)),
)
.returns(T::Array[T.type_parameter(:U)])
def map(&blk); end
# File lib/types/_types.rb, line 118
def self.type_parameter(name)
  T::Types::TypeParameter.new(name)
end
unsafe(value) click to toggle source

For the static type checker, strips all type information from a value and returns the same value, but statically-typed as `T.untyped`. Can be used to tell the static checker to “trust you” by discarding type information you know to be incorrect. Use with care! (This has no effect at runtime.)

We can't actually write this sig because we ourselves are inside the `T::` module and doing this would create a bootstrapping cycle. However, we also don't actually need to do so; An untyped identity method works just as well here.

sig {params(value: T.untyped).returns(T.untyped)}

# File lib/types/_types.rb, line 189
def self.unsafe(value)
  value
end
untyped() click to toggle source

Matches any object. In the static checker, T.untyped allows any method calls or operations.

# File lib/types/_types.rb, line 41
def self.untyped
  T::Types::Untyped::Private::INSTANCE
end