Open3

typed_struct

pockepocke

ランタイムでinitialize時(多分)に引数の型チェックをするstruct。
structを継承して作っている。

型にはクラスのオブジェクトを指定できる他、RBSを文字列として書ける。
RBSを書いた場合、それを内部でparseして型チェックをしているっぽい。

RBSは構文木レベルで使っているのかなと思ったけど、見てみたらDefinitionBuilderとかも使っているぽい。コードを読んでいたら型aliasの解決にはDefinitionBuilderが必要だなと気がついて、たしかにという感じ。

https://github.com/johansenja/typed_struct/blob/5a70fc44b1d4d0d08248fb92f2949481441bef7c/lib/typed_struct.rb#L53-L55
トップレベルにdef Rbsをしているのが厳しい。

pockepocke

Steepとかでの静的型解析のためのサポートはなさそう。たとえば次のようなものを想像した。

# from README
User = TypedStruct.new name: String, # an instance of String
                       age: Integer, # an instance of Integer
                       username: /\w{4,}/, # must match given Regexp
                       rating: (0..5), # must be value from 0 to 5
                       type: "User", # must by a string with value "User"
                       interests: Rbs("Array[String]"), # an RBS generic type (an Array of Strings)
                       preferences: Rbs("{ opt_out_of_emails: bool, additional: untyped }") # RBS record type

User.to_rbs # => print User class definition in RBS