Thanks, but I'm not sure I can do that, the `Container` is declared before and 
in separate module than the `GetPriceJob`, so this `Container[T:Job | 
GetPriceJob]` declaration is impossible.

The real example - I'm building a `Crawler` that should handle different types 
of `Jobs`. There's the `lib/crawler` module and everyone can use it if it 
declares the `proc process[J: Job](job: J): void` function. So the specific 
type like `GetPriceJob` is declared after, it's unknown to the `lib/crawler` 
module.

The unexpected thing is that it actually works if no type restriction is used. 
The whole example, play with it online 
<https://repl.it/@alexeypetrushin/crawler#main.nim>

Code for `lib/crawler.nim`
    
    
    type
      Job* = object of RootObj
        id*: string
      
      CrawlerRef*[J] = ref object
        job: J
    
    proc new_crawler*[J](job: J): CrawlerRef[J] =
      CrawlerRef[J](job: job)
    
    proc run*[J](crawler: CrawlerRef[J]): void =
      # Why this version won't work?
      # let v: string = process(crawler.job)
      
      let v: string = crawler.job.process()
      echo crawler.job.id, "=", v
    
    
    Run

Code for `main.nim`
    
    
    import lib/crawler
    
    type GetStockPrice = object of Job
      value: string
    
    proc process(job: GetStockPrice): string =
      job.value
    
    var price_crawler = new_crawler(
      job = GetStockPrice(id: "MSFT", value: "500USD")
    )
    price_crawler.run()
    
    
    Run

Reply via email to