我在定义一个包含auto类型的概念时遇到了困难。看起来尼姆在抱怨type T = auto变成了untyped。
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 而不是这一事实之外,):
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.}我基本上是从这里的文档中复制过来的。我做错了什么?
发布于 2021-06-13 14:54:32
我不知道如何回答您关于在概念体中使用auto的问题,我的直觉是,这不太可能起作用。
我可以通过移除自动内容和调用带有the参数的map的任何操作来使您的示例工作。我也不明白。
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))https://stackoverflow.com/questions/67958826
复制相似问题