当我对方法使用双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时的预期行为吗?我在水晶书中找不到任何关于此事的内容:https://crystal-lang.org/docs/syntax_and_semantics/splats_and_tuples.html
答案 0 :(得分:3)
这是一个错误,请按此报告。
请注意,建议不要使用类型声明局部变量。因为它是最近添加的,所以它没有经过良好测试,显然容易出错。
无论如何,你可以看到这是有效的:
def show(**attrs)
place = "w"
puts place
puts attrs[:place]
end
show(name: "Bobby", place: "World")
w
World