Проблема с определением концепции Nim с автоматическим типом внутри

У меня возникли проблемы с определением концепции с типом 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.}

В основном я копирую и вставляю документы здесь. Что я делаю не так?


person schneiderfelipe    schedule 13.06.2021    source источник
comment
похоже, что этот пример никогда не работал должным образом github.com/nim-lang/Nim /вопросы/5650   -  person shirleyquirk    schedule 13.06.2021


Ответы (1)


Я не знаю, как ответить на ваш вопрос об использовании auto внутри тела концепции, мне кажется, что это вряд ли сработает.

Я могу заставить ваш пример работать, удалив автоматический материал и все, что происходит с вызовом map с параметром typedef? Я тоже этого не понимаю.

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))
person shirleyquirk    schedule 13.06.2021
comment
Эй, спасибо ????! Очень интересно, и проблема, похоже, действительно в ключевом слове auto. Я позволил себе опубликовать ваш пример в упомянутой проблеме. . Я также ссылаюсь на игровую площадку. - person schneiderfelipe; 13.06.2021