首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义内有自动类型的Nim概念时遇到麻烦

定义内有自动类型的Nim概念时遇到麻烦
EN

Stack Overflow用户
提问于 2021-06-13 13:33:00
回答 1查看 187关注 0票数 1

我在定义一个包含auto类型的概念时遇到了困难。看起来尼姆在抱怨type T = auto变成了untyped

下面是一个基本取自从医生那里的最小示例( 从医生那里):

代码语言:javascript
复制
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 而不是这一事实之外,):

代码语言:javascript
复制
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.}

我基本上是从这里的文档中复制过来的。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-06-13 14:54:32

我不知道如何回答您关于在概念体中使用auto的问题,我的直觉是,这不太可能起作用。

我可以通过移除自动内容和调用带有the参数的map的任何操作来使您的示例工作。我也不明白。

代码语言:javascript
复制
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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67958826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档