ユースケース
継承時に利用。使わないと子クラスが親クラスを返す書き方になってしまう。
コードについて
例として載せているコードはほぼドキュメントまま
インスタンスメソッドの場合
T.self_typeを使用する
1
2
3
4
5
6
7
8
9
10
11
12
13
| class Parent
extend T::Sig
sig {returns(T.self_type)}
def foo
self
end
end
class Child < Parent; end
T.reveal_type(Parent.new.foo) # => Parent
T.reveal_type(Child.new.foo) # => Child
|
クラスメソッドの場合
T.attached_classを使用する
1
2
3
4
5
6
7
8
9
10
11
| class Parent
sig {returns(T.attached_class)}
def self.make
new
end
end
class Child < Parent; end
T.reveal_type(Parent.make) # => Parent
T.reveal_type(Child.make) # => Child
|
参考