我熟悉 Ruby 中的类型注释。但是,在任何地方都没有提到关于 nilable 类型的内容。也就是说,类似于以下内容:
class Merchant
attr_reader token: String # never nil
attr_reader name: String # how to signal it that it may be nil?
[.......]
end
反之亦然:我希望能够描述一个不允许 nil 值的类型。
那可能吗?如何?
更新1:
irb(main):009:0> tp1 = String
=> String
irb(main):010:0> v1 = "fdsafds"
=> "fdsafds"
irb(main):011:0> tp1 === v1
=> true
irb(main):012:0> tp2 = String?
Traceback (most recent call last):
1: from (irb):12:in `<main>'
NoMethodError (undefined method `String?' for main:Object)
Did you mean? String
为什么会失败?