Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler can't find generic items when iterating over a field in a generic object #14459

Closed
ghost opened this issue May 26, 2020 · 2 comments
Closed

Comments

@ghost
Copy link

ghost commented May 26, 2020

This code works only if you replace cache.order with items(cache.order). Title may not be fully correct so feel free to edit it. Found by @PMunch in their https://github.com/PMunch/nim-cache project

Example

# a.nim
type
  Data[T] = object
    data: seq[T]
  
  Cache*[X: static[int]] = ref object
    order: Data[string]

iterator items[T](x: Data[T]): T = 
  for x in x.data:
    yield x

proc initCache*[X: static[int]](): Cache[X] =
  Cache[X](order: Data[string](data: @["hello"]))

iterator items*[X: static[int]](cache: Cache[X]): string =
  # works if you replace cache.order with items(cache.order)
  for item in cache.order:
    yield item

# b.nim
import a

var c = initCache[5]()
for entry in c:
  echo entry

Current Output

/home/dian/Projects/nim-mathexpr/nim-cache/a.nim(17, 20) Error: type mismatch: got <Data[system.string]>
but expected one of: 
iterator items(E: typedesc[enum]): E:type
  first type mismatch at position: 1
  required type for E: type enum
  but expression 'cache.order' is of type: Data[system.string]
iterator items(a: cstring): char
  first type mismatch at position: 1
  required type for a: cstring
  but expression 'cache.order' is of type: Data[system.string]
iterator items(a: string): char
  first type mismatch at position: 1
  required type for a: string
  but expression 'cache.order' is of type: Data[system.string]
iterator items[IX, T](a: array[IX, T]): T
  first type mismatch at position: 1
  required type for a: array[IX, T]
  but expression 'cache.order' is of type: Data[system.string]
iterator items[T](a: openArray[T]): T
  first type mismatch at position: 1
  required type for a: openArray[T]
  but expression 'cache.order' is of type: Data[system.string]
iterator items[T](a: seq[T]): T
  first type mismatch at position: 1
  required type for a: seq[T]
  but expression 'cache.order' is of type: Data[system.string]
iterator items[T](a: set[T]): T
  first type mismatch at position: 1
  required type for a: set[T]
  but expression 'cache.order' is of type: Data[system.string]
iterator items[T](s: HSlice[T, T]): T
  first type mismatch at position: 1
  required type for s: HSlice[items.T, items.T]
  but expression 'cache.order' is of type: Data[system.string]
iterator items[X: static[int]](cache: Cache[X]): string
  first type mismatch at position: 1
  required type for cache: Cache[items.X]
  but expression 'cache.order' is of type: Data[system.string]

expression: items(cache.order)

Expected Output

hello
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-05-26
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: dadc97d91e6ece6387195bdaad54baa9d3f0fc12
active boot switches: -d:release
@nc-x
Copy link
Contributor

nc-x commented May 28, 2020

Same as #4773, #11167, #4722 i think

@timotheecour
Copy link
Member

ya it's a duplicate; closing but feel free to reopen if you disagree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants