i have a class
class Node
@size : Int32?
@word : String?
property word
def size : Int32
@size ||= @word.not_nil!.size
@size
end
end
n = Node.new
n.word = "hello"
p n.size
when i execute crystal src/node.cr
i got error
In src/node.cr:5:16
5 | def size : Int32
^
Error: method must return Int32 but it is returning (Int32 | Nil)
then , i change code to this
class Node
@size : Int32?
@word : String?
property word
def size : Int32
@size ||= @word.not_nil!.size
return 0 if @size.nil?
@size.to_i32
end
end
n = Node.new
n.word = "hello"
p n.size
got the same error. What am I supposed to do?