使用内部自动类型定义 Nim 概念时遇到问题 [英] Trouble defining a Nim concept with an auto type inside
问题描述
我在定义包含 auto
类型的概念时遇到问题.看起来 Nim 在抱怨 type T = auto
变成了 untyped
.
I'm having trouble defining a concept with an auto
type inside.
It looks like Nim is complaining that type T = auto
becomes untyped
.
Here is a minimal example (run it online here) taken basically from from the docs):
import sugar, typetraits
type
Functor[A] {.explain.} = concept f
type MatchedGenericType = genericHead(typeof(f))
# `f` will be a value of a type such as `Option[T]`
# `MatchedGenericType` will become the `Option` type
# f.val is A
# The Functor should provide a way to obtain
# a value stored inside it
type T = auto
map(f, A -> T) is MatchedGenericType[T]
# And it should provide a way to map one instance of
# the Functor to an instance of a different type, given
# a suitable `map` operation for the enclosed values
import options
echo Option[int] is Functor # should print true but doesn't!
# The above came straight from
# <https://nim-lang.org/docs/manual_experimental.html#concepts-generic-concepts-and-type-binding-rules>
proc f(x: Functor) = echo "yes!"
f(some(1))
这是错误的相关部分(除了文档说 Option[int] is Functor
应该是 true
但不是强>):
Here's the relevant part of the error (beside the fact that the docs say Option[int] is Functor
should be true
but isn't):
proc map[T, R](self: Option[T]; callback: proc (input: T): R): Option[R]
first type mismatch at position: 2
required type for callback: proc (input: T): R{.closure.}
but expression 'proc (i0: A): T' is of type: type proc (i0: int): untyped{.closure.}
我基本上是从文档中复制粘贴.我做错了什么?
I'm basically copy-pasting from the docs here. What am I doing wrong?
推荐答案
我不知道如何回答您关于在概念体中使用 auto
的问题,我的直觉是这不太可能工作.
I don't know how to answer your question about using auto
inside a concept body, my gut is that is rather unlikely to work.
我可以通过删除自动内容以及使用 typedef 参数调用 map
发生的任何事情来使您的示例工作?我也不明白.
I can get your example to work by removing the auto stuff and whatever's going on with calling map
with a typedef parameter? I don't understand that either.
import sugar, typetraits
type
Functor[A] {.explain.} = concept f,type t
type MatchedGenericType = genericHead(typeof(f))
# `f` will be a value of a type such as `Option[T]`
# `MatchedGenericType` will become the `Option` type
# f.val is A
# The Functor should provide a way to obtain
# a value stored inside it
#type T = auto
proc mapto[T](x:t):MatchedGenericType[T] = map(f,default(A->T))
#map(f, A->T) is MatchedGenericType[T]
# And it should provide a way to map one instance of
# the Functor to a instance of a different type, given
# a suitable `map` operation for the enclosed values
import options
echo Option[int] is Functor # prints true
# The above came straight from <https://nim-lang.org/docs/manual_experimental.html#concepts-generic-concepts-and-type-binding-rules>.
proc f(x: Functor) = echo "yes!"
f(some(1))
这篇关于使用内部自动类型定义 Nim 概念时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!