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

View file

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