This code
struct Person
getter first : String
getter family : String?
def initialize(@first)
end
def initialize(@first, @family)
end
end
if Random.rand < 0.5
prs = Person.new("Foo")
else
prs = Person.new("Foo", "Bar")
end
if prs.family.nil?
puts "nil"
else
puts "not nil"
puts prs.family.size
end
gives me this error:
20 | puts prs.family.size
^---
Error: undefined method 'size' for Nil (compile-time type is (String | Nil))
however this works:
if Random.rand < 0.5
x = "abc"
else
x = nil
end
if x.nil?
puts "nil"
else
puts x
puts x.size
end
Why is the first one not working and how can I make Struct attributes Nillable and avoid this problem?