From be420ec875211cfc2be7f3b0e1d95435e0d079de Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Tue, 15 Mar 2016 20:49:36 -0300 Subject: [PATCH] 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. --- src/radix/result.cr | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/radix/result.cr b/src/radix/result.cr index d58fc0f..ca93480 100644 --- a/src/radix/result.cr +++ b/src/radix/result.cr @@ -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`.