double splats 在方法上有不同的处理方式吗?
Are double splats treated differently in methods?
当我对一个方法使用双 splat 时,不允许我在方法中定义一个类型的变量:
def show(**attrs)
place : String = "w"
puts place
end
show(name: "Bobby") # This works.
show(name: "Bobby", place: "World") # This fails:
#
#in tmp.cr:2: variable 'place' already declared
#
# place : String = "w"
# ^~~~~
这是使用双 splats 时的预期行为吗?我在 Crystal 书中找不到任何关于此的内容:https://crystal-lang.org/docs/syntax_and_semantics/splats_and_tuples.html
这是一个错误,请按此报告。
请注意,不推荐使用类型声明局部变量。因为它是最近添加的,所以它没有经过很好的测试并且显然容易出现错误。
无论如何,你可以看到这有效:
def show(**attrs)
place = "w"
puts place
puts attrs[:place]
end
show(name: "Bobby", place: "World")
w
World
当我对一个方法使用双 splat 时,不允许我在方法中定义一个类型的变量:
def show(**attrs)
place : String = "w"
puts place
end
show(name: "Bobby") # This works.
show(name: "Bobby", place: "World") # This fails:
#
#in tmp.cr:2: variable 'place' already declared
#
# place : String = "w"
# ^~~~~
这是使用双 splats 时的预期行为吗?我在 Crystal 书中找不到任何关于此的内容:https://crystal-lang.org/docs/syntax_and_semantics/splats_and_tuples.html
这是一个错误,请按此报告。
请注意,不推荐使用类型声明局部变量。因为它是最近添加的,所以它没有经过很好的测试并且显然容易出现错误。
无论如何,你可以看到这有效:
def show(**attrs)
place = "w"
puts place
puts attrs[:place]
end
show(name: "Bobby", place: "World")
w
World