Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 2.36 KB

class-of.md

File metadata and controls

75 lines (54 loc) · 2.36 KB
id title
class-of
T.class_of

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

T.class_of(Integer)

It can be confusing whether you want MyClass or T.class_of(MyClass). For reference, these assertions are true:

T.let(5, Integer)

T.let(5.class, T.class_of(Integer))
T.let(Integer, T.class_of(Integer))

You can also use modules. It respects inheritance in the same way that Class Types do:

extend T::Sig

class Grandparent; end
class Parent < Grandparent; end
class Child < Parent; end

sig {params(x: T.class_of(Parent)).void}
def foo(x); end

foo(Parent) # ok
foo(Child) # ok

foo(Grandparent) # error
→ View on sorbet.run

Gotcha: module ancestor chains (include, extend) work in a way many people don't expect:

# typed: true

extend T::Sig

module M
  def self.foo; end
end
class C; include M; end

sig {params(x: T.class_of(M)).void}
def test(x)
  x.foo
end


M.foo                      # ok
M.is_a?(M.singleton_class) # => true
test(M)                    # ok, given above

C.foo                      # not ok: this raises a NoMethodError when run
C.is_a?(M.singleton_class) # => false
test(C)                    # not ok, given above
→ View on sorbet.run