module T
def self.absurd(value)
happens. Commonly used to assert that a case or if statement exhausts all
A way to ask Sorbet to prove that a certain branch of control flow never
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, {kind: 'T.absurd', value: value, type: nil}) end end
def self.all(type_a, type_b, *types)
def self.all(type_a, type_b, *types) T::Types::Intersection.new([type_a, type_b] + types) end
def self.any(type_a, type_b, *types)
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
def self.anything
def self.anything T::Types::Anything::Private::INSTANCE end
def self.assert_type!(value, type, checked: true)
statically known and being checked appropriately. If `checked` is true, raises an exception at
fail). Use this for debugging typechecking errors, or to ensure that type information is
Tells the typechecker to ensure that `value` is of type `type` (if not, the typechecker will
def self.assert_type!(value, type, checked: true) return value unless checked Private::Casts.cast(value, type, "T.assert_type!") end
def self.attached_class
def self.attached_class T::Types::AttachedClassType::Private::INSTANCE end
def self.bind(value, type, checked: true)
If `checked` is true, raises an exception at runtime if the value
`T.bind` behaves like `T.cast` in that it is assumed to be true statically.
end
...
T.bind(self, NewBinding)
seconds = lambda do
Use like:
Useful for blocks that are captured and executed later with instance_exec.
Tells the type checker to treat `self` in the current block as `type`.
def self.bind(value, type, checked: true) return value unless checked Private::Casts.cast(value, type, "T.bind") end
def self.cast(value, type, checked: true)
exception at runtime if the value doesn't match the type.
an expression that the typechecker is unable to analyze. If `checked` is true, raises an
Tells the typechecker that `value` is of type `type`. Use this to get additional checking after
def self.cast(value, type, checked: true) return value unless checked Private::Casts.cast(value, type, "T.cast") end
def self.class_of(klass)
Matches any class that subclasses or includes the provided class
def self.class_of(klass) T::Types::ClassOf.new(klass) end
def self.deprecated_enum(values)
- Use T::Enum instead.
def self.deprecated_enum(values) T::Types::Enum.new(values) end
def self.let(value, type, checked: true)
If `checked` is true, raises an exception at runtime if the value
Compared to `T.cast`, `T.let` is _checked_ by static system.
seconds = T.let(0.0, Float)
like:
Tells the typechecker to declare a variable of type `type`. Use
def self.let(value, type, checked: true) return value unless checked Private::Casts.cast(value, type, "T.let") end
def self.must(arg)
to contain a non-nil value at this point.
Intended to be used to promise sorbet that a given nilable value happens
needs_foo(foo)
raise "nil" if foo.nil?
foo = maybe_gives_foo
Equivalent to:
needs_foo(T.must(maybe_gives_foo))
Intended to be used as:
otherwise.
A convenience method to `raise` when the argument is `nil` and return it
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, {kind: 'T.must', value: arg, type: nil}) end end
def self.must_because(arg)
to contain a non-nil value at this point.
Intended to be used to promise sorbet that a given nilable value happens
needs_foo(foo)
raise "reason_foo_should_not_be_nil" if foo.nil?
foo = maybe_gives_foo
Equivalent to:
needs_foo(T.must_because(maybe_gives_foo) {"reason_foo_should_not_be_nil"})
Intended to be used as:
is `nil` and return it otherwise.
A convenience method to `raise` with a provided error reason when the argument
def self.must_because(arg) return arg if arg return arg if arg == false begin raise TypeError.new("Unexpected `nil` because #{yield}") rescue TypeError => e # raise into rescue to ensure e.backtrace is populated T::Configuration.inline_type_error_handler(e, {kind: 'T.must_because', value: arg, type: nil}) end end
def self.nilable(type)
def self.nilable(type) T::Types::Union::Private::Pool.union_of_types(T::Utils.coerce(type), T::Utils::Nilable::NIL_TYPE) end
def self.noreturn
def self.noreturn T::Types::NoReturn::Private::INSTANCE end
def self.proc
def self.proc T::Private::Methods.start_proc end
def self.reveal_type(value)
This can be useful for debugging and checking assumptions.
A way to ask Sorbet to show what type it thinks an expression has.
def self.reveal_type(value) value end
def self.self_type
def self.self_type T::Types::SelfType::Private::INSTANCE end
def self.type_alias(type=nil, &blk)
be printed with reference to the underlying type.
The name of the type alias is not preserved; Error messages will
end
arg || default
def or_else(arg, default)
sig {params(arg: NilableString, default: String).returns(String)}
NilableString = T.type_alias {T.nilable(String)}
@example
is needed for support by the static checker.
wrapper that contains a proc that is evaluated to get the underlying type. This syntax however
Constructs a type alias. Used to create a short name for a larger type. In Ruby this returns a
def self.type_alias(type=nil, &blk) if blk T::Private::Types::TypeAlias.new(blk) else T::Utils.coerce(type) end end
def self.type_parameter(name)
.returns(T::Array[T.type_parameter(:U)])
)
blk: T.proc.params(arg0: Elem).returns(T.type_parameter(:U)),
.params(
.type_parameters(:U)
sig
@example
This is used for generic methods.
`type_parameters`.
References a type parameter which was previously defined with
def self.type_parameter(name) T::Types::TypeParameter.make(name) end
def self.unsafe(value)
identity method works just as well here.
cycle. However, we also don't actually need to do so; An untyped
the `T::` module and doing this would create a bootstrapping
We can't actually write this sig because we ourselves are inside
(This has no effect at runtime.)
you know to be incorrect. Use with care!
Can be used to tell the static checker to "trust you" by discarding type information
and returns the same value, but statically-typed as `T.untyped`.
For the static type checker, strips all type information from a value
def self.unsafe(value) value end
def self.untyped
Matches any object. In the static checker, T.untyped allows any
def self.untyped T::Types::Untyped::Private::INSTANCE end