【发布时间】:2021-09-07 00:12:56
【问题描述】:
我正在尝试将 Ruby 的函数组合运算符 << 实现到 Crystal 的 proc 中。在 Ruby 中,这似乎很简单。
def << block
proc { |*args| self.call( block.to_proc.call(*args) ) }
end
end
我尝试过做类似的事情。
struct Proc
def <<(&block)
Proc.new { |*args, blk| call(block.call(*args, blk)) }
end
end
我试过用一个简单的加法器和子函数来测试它
def add(x : Int32)
x + 1
end
def sub(x : Int32)
x - 1
end
但是,我收到了这个错误。 Error: wrong number of arguments for 'Proc(Int32, Int32)#<<' (given 1, expected 0)
我还尝试更改 << 以接收 proc,但这也会导致 expected block type to be a function type, not Proc(*T, R)
我对这门语言有点陌生,所以我不太确定我缺少哪些知识来理解为什么这不起作用。
【问题讨论】:
标签: crystal-lang