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

Error when trying to iterate a distinct type based array #7167

Closed
dawkot opened this issue Jan 31, 2018 · 4 comments
Closed

Error when trying to iterate a distinct type based array #7167

dawkot opened this issue Jan 31, 2018 · 4 comments
Labels

Comments

@dawkot
Copy link

dawkot commented Jan 31, 2018

type Id = distinct range[0..1_000]

var xs: array[Id, bool]

for x in xs: discard # type mismatch: got (T) but expected 'bool'
@Araq
Copy link
Member

Araq commented Feb 1, 2018

Why should it work? The distinct type means arithmetic operations are not available.

@Araq Araq closed this as completed Feb 1, 2018
@GULPF
Copy link
Member

GULPF commented Feb 1, 2018

@Araq I disagree. Since all ordinal types are allowed as array indices, they should work with the items iterator even if they are distinct. Is there a reason why the iterator can't be implemented like this?

iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
  ## iterates over each item of `a`.
  var i = low(IX).IX
  if ord(i) <= ord(high(IX)):
    while true:
      yield a[i]
      if ord(i) >= ord(high(IX)): break
      inc(i)

Having to borrow >= to use a distinct type as array indexer just seems like a leaky abstraction.

@Araq
Copy link
Member

Araq commented Feb 1, 2018

The real problem here is that distinct T is an ordinal if T is ordinal but that makes no sense. A distinct type should not be an ordinal. I tried to fix that in the compiler but times.nim now depends on it. -.-

@Araq Araq reopened this Feb 1, 2018
@GULPF
Copy link
Member

GULPF commented Feb 1, 2018

Agreed that distinct T shouldn't automatically be ordinal if T is ordinal, but imo there should be a way to explicitly inherit the "ordinalness" since it can be useful. Fwiw I don't think times depends on it after #6978, since it changes the Time type to be an object (which will undoubtedly break a lot of code...).

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

No branches or pull requests

3 participants