Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 表达式';T';类型为';输入int';而且必须被抛弃_Templates_Nim Lang - Fatal编程技术网

Templates 表达式';T';类型为';输入int';而且必须被抛弃

Templates 表达式';T';类型为';输入int';而且必须被抛弃,templates,nim-lang,Templates,Nim Lang,假设我只希望模板从泛型参数“生成”类型,并在需要类型的地方使用模板的调用: template p[T] = T var a: p[int]() (3,14)错误:表达式“T”的类型为“type int”,必须为 丢弃 哈哈,真的吗? 我希望我只是一个新手,确实有一个方法(希望没有争议)来做到这一点 请注意,它是相同的输出,具有非泛型尝试: template p(t: typedesc) = t var a: p(int) 编辑:阅读时,我意识到如果我们指定模板的返回类型,系统可能会感觉更受欢

假设我只希望模板从泛型参数“生成”类型,并在需要类型的地方使用模板的调用:

template p[T] = T
var a: p[int]()
(3,14)错误:表达式“T”的类型为“type int”,必须为 丢弃

哈哈,真的吗?
我希望我只是一个新手,确实有一个方法(希望没有争议)来做到这一点

请注意,它是相同的输出,具有非泛型尝试:

template p(t: typedesc) = t
var a: p(int)
编辑:阅读时,我意识到如果我们指定模板的返回类型,系统可能会感觉更受欢迎;添加
:在
=t
之前取消键入
,以生成以前的代码段。有什么解释吗

template p[T] = T
var a: p[int]()
这与:

template p[T]: void = T
var a: p[int]()
你告诉编译器你的模板什么也不返回,这就是它抱怨的原因

所以您需要指定返回类型

template p[T]: typedesc = T
var a: p[int]()
那么它工作得很好。这种行为扩展到Nim中的过程和方法,不指定返回类型意味着没有返回值

这与:

template p[T]: void = T
var a: p[int]()
你告诉编译器你的模板什么也不返回,这就是它抱怨的原因

所以您需要指定返回类型

template p[T]: typedesc = T
var a: p[int]()

那么它工作得很好。这种行为扩展到Nim中的过程和方法,不指定返回类型意味着没有返回值。

在Nim中从类型映射到类型的编译时函数通常使用。与泛型参数相比,它还有一个额外的好处,即允许以不同的方式提供处理不同类型的多个重载:

type
  Foo = object

# handler all integer types:
template myTypeMapping(T: typedesc[SomeInteger]): typedesc = string 

# specific handling of the Foo type:
template myTypeMapping(T: typedesc[Foo]): typedesc = seq[string]

# all sequence types are not re-mapped:
template myTypeMapping(T: typedesc[seq]): typedesc = T

请注意,您始终需要指定模板具有
typedesc
返回类型。

在Nim中,从类型到类型的编译时函数映射通常使用。与泛型参数相比,它还有一个额外的好处,即允许以不同的方式提供处理不同类型的多个重载:

type
  Foo = object

# handler all integer types:
template myTypeMapping(T: typedesc[SomeInteger]): typedesc = string 

# specific handling of the Foo type:
template myTypeMapping(T: typedesc[Foo]): typedesc = seq[string]

# all sequence types are not re-mapped:
template myTypeMapping(T: typedesc[seq]): typedesc = T

请注意,您始终需要指定模板具有
typedesc
返回类型。

通用参数便于推导。我们用typedesc参数就失去了这一点?好吧,只有当模板有其他参数时才能推断出它们。如果类型本身是必需的参数,那么我的建议是始终使用
typedesc
。泛型参数可以方便地进行推导。我们用typedesc参数就失去了这一点?好吧,只有当模板有其他参数时才能推断出它们。如果类型本身是必需的参数,那么我的建议是始终使用
typedesc