简体   繁体   中英

Define generic function type

Given the following snippet:

type
  Base = object of RootObj
  Consume*[T: Base] = proc(e: T): bool

I want to define several types from base and base consumer for it, like this:

type
  BaseOne = object of Base
  ConsumeOne = Consume[BaseOne]

Now, when i create a new ConsumeOne :

let co: ConsumeOne = proc(b: BaseOne): bool = false

I get the following compiler error: Error: type mismatch: got (proc (b: BaseOne): bool{.gcsafe, locks: 0.}) but expected 'ConsumeOne'

What am i doing wrong?

The problem is that type class constraints for type parameters currently do not match subtypes; I do not know if that is a bug or intentional. For now, simply remove the : Base constraint from the type parameter T .

也许有助于制作消费gcsafe:

Consume*[T: Base] = proc(e: T): bool {.gcsafe.}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM