Nim 返回包含 proc 的自定义元组 [英] Nim return custom tuple containing proc

查看:24
本文介绍了Nim 返回包含 proc 的自定义元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 proc 返回一个自定义元组,该元组包含一个 proc 类型的单个元素,即

I'm trying to create a proc that returns a custom tuple that contains a single element that is a proc type i.e.

type
  CustomTuple = tuple
    foo: proc(input: int): int

proc createCustomTuple(): CustomTuple =
  (foo: proc(input: int): int = 10)

但是,当我编译它时,我收到以下错误(我在 Windows 上使用 Nim 1.2.6 版进行编译).

However, when I compile this I get the following error (I am compiling with Nim Version 1.2.6 on Windows).

错误:类型不匹配:得到 <tuple[foo: proc (input: int):int{.noSideEffect, gcsafe, locks: 0.}]>但预期 'CustomTuple =元组[foo: proc (input: int): int{.closure.}]'

Error: type mismatch: got <tuple[foo: proc (input: int): int{.noSideEffect, gcsafe, locks: 0.}]> but expected 'CustomTuple = tuple[foo: proc (input: int): int{.closure.}]'

所以编译器认为我返回的是一个常规元组而不是 CustomTuple 但我不知道如何更改它以使其工作.Nim 手册 中的元组文档自定义元组是按照我的方式构造的,我找不到任何从 proc 返回自定义元组的示例.

So the compiler thinks I am returning a regular tuple and not a CustomTuple but I have no idea how to change this to make it work. The documentation for tuples in the Nim manual show custom tuples being constructed in the way that I am doing it and I couldn't find any examples of returning a custom tuple from a proc.

如果我将我的 CustomTuple 定义更改为包含不是 procs 的类型,那么它会成功编译,因此看起来它有一些事情要做我的自定义元组,其中包含导致无法编译的 proc.

If I change my CustomTuple definition to contain types that aren't procs then it compiles successfully so it appears it has something to do my custom tuple containing a proc that is causing this to fail to compile.

谁能解释为什么上面的代码不能编译?

Can anyone explain why the above code is not compiling?

推荐答案

我认为我的原始代码无法正常工作的原因是 Nim 无法在将 proc 添加到元组时自动将其转换为闭包.Nim 论坛中对此有一些讨论.

I think the reason my original code isn't working is because Nim is unable to convert the proc to a closure automatically when it is added to a tuple. There is some discussion around this in the Nim forums.

由于 proc 未转换为闭包,编译器无法确定返回的元组是 CustomTuple,因为类型不匹配,这解释了错误消息.

As the proc is not converted to a closure the compiler is unable to determine that the tuple being returned is a CustomTuple because the types don't match which explains the error message.

因此,当在元组中打包 proc 时,您需要将其显式转换为闭包.这可以通过像这样显式转换 proc 来完成:

So when packing a proc in a tuple you need to explicitly convert it to a closure. This can be done by explicitly casting the proc like this:

type
  CustomTuple = tuple
    foo: proc(input: int): int

proc createCustomTuple(): CustomTuple =
  (foo: (proc(input:int):int)(proc(input: int): int = 10))

或者通过添加这样的 {.closure.} 编译指示(我认为它更简洁).

or by adding a {.closure.} pragma like this (which I think is much cleaner).

type
  CustomTuple = tuple
    foo: proc(input: int): int

proc createCustomTuple(): CustomTuple =
  (foo: proc(input: int): int {.closure.} = 10)

如果您不想执行其中任何一个,您可以根据 Salewski 的回答将 proc 分配给隐式 result 变量的 foo 属性.

If you don't want to do either of these you can assign the proc to the foo property of the implicit result variable as per Salewski's answer.

这也解释了为什么 Salewski 将 proc 分配给隐式 result 变量的 foo 属性的原始解决方案有效;在这种情况下,编译器能够自动将 proc 转换为闭包.

This also explains why Salewski's original solution of assigning the proc to the foo property of the implicit result variable worked; the compiler is able to automatically convert the proc to a closure in that case.

这篇关于Nim 返回包含 proc 的自定义元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆