Fix incorrect return type

`Result#key` is expected to be `String`, yet Crystal considered
that by branching is possible result to be `nil`.

This was caused by *return-early* approach:

    def foo
      return @foo if @foo

      @foo = "awesome"
    end

    pp typeof(foo) # => String?

Changed to instead use memoization technique.
This commit is contained in:
Luis Lavena 2016-03-15 20:49:36 -03:00
parent 2c9279e51e
commit be420ec875
1 changed files with 7 additions and 9 deletions

View File

@ -61,15 +61,13 @@ module Radix
# # => ""
# ```
def key : String
return @key if @key
key = String.build { |io|
@nodes.each do |node|
io << node.key
end
}
@key = key
@key ||= begin
String.build { |io|
@nodes.each do |node|
io << node.key
end
}
end
end
# Adjust result information by using the details of the given `Node`.