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

Generic function works in module but importing it fails #16468

Closed
oakes opened this issue Dec 25, 2020 · 1 comment
Closed

Generic function works in module but importing it fails #16468

oakes opened this issue Dec 25, 2020 · 1 comment

Comments

@oakes
Copy link

oakes commented Dec 25, 2020

The following code works:

import sets, sequtils

proc dedupe*[T](arr: openArray[T]): seq[T] =
  arr.toHashSet.toSeq

echo dedupe([1, 2, 3])

But if I move the proc to its own module, named foo, and try to import it like this...

import foo

echo dedupe([1, 2, 3])

I get this error:

nim_misc.nim(3, 12) template/generic instantiation of `dedupe` from here
nim_misc/src/foo.nim(4, 6) Error: type mismatch: got <HashSet[system.int]>
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 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items(a: cstring): char
  first type mismatch at position: 1
  required type for a: cstring
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items(a: string): char
  first type mismatch at position: 1
  required type for a: string
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items[IX, T](a: array[IX, T]): T
  first type mismatch at position: 1
  required type for a: array[IX, T]
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items[T: char](a: openArray[T]): T
  first type mismatch at position: 1
  required type for a: openArray[T: char]
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items[T: not char](a: openArray[T]): lent2 T
  first type mismatch at position: 1
  required type for a: openArray[T: not char]
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items[T](a: seq[T]): lent2 T
  first type mismatch at position: 1
  required type for a: seq[T]
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
iterator items[T](a: set[T]): T
  first type mismatch at position: 1
  required type for a: set[T]
  but expression 'toHashSet(arr)' is of type: HashSet[system.int]
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 'toHashSet(arr)' is of type: HashSet[system.int]

Expected Output

The import should work without error.

Possible Solution

  • If you put import sets before the import foo, it works.
  • In the foo module, if you do export sets.items it works.
  • If you make dedupe a non-generic function (declaring it as proc dedupe*(arr: openArray[int]): seq[int] for example) it works.

Additional Information

$ nim -v
Nim Compiler Version 1.4.2 [MacOSX: amd64]
Compiled at 2020-12-21
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release
@Clyybber
Copy link
Contributor

Duplicate of #11167

@Clyybber Clyybber marked this as a duplicate of #11167 Dec 25, 2020
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