小编Mik*_*eev的帖子

有没有办法在Crystal中使用带有splat运算符的非元组对象?

是否有一些函数或语法结构使下一个例子有效?

Hash#values_at使用Array参数调用函数:

h = {"a" => 1, "b" => 2, "c" => 3}
ary = ["a", "b"]
h.values_at(*ary) # Error: argument to splat must be a tuple, not Array(String)
Run Code Online (Sandbox Code Playgroud)

传递Hash给初始化类或结构:

struct Point                                                                                                 
    def initialize(@x : Int32, @y : Int32)                                                                     
    end                                                                                                        
end
h = {"x" => 1, "y" => 2}
Point.new(**h) # Error: argument to double splat must be a named tuple, not Hash(String, Int32)
Run Code Online (Sandbox Code Playgroud)

splat crystal-lang

2
推荐指数
1
解决办法
164
查看次数

标签 统计

crystal-lang ×1

splat ×1