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

Comments

Projects
None yet
3 participants
@dawkot
Copy link

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

This comment has been minimized.

Copy link
Member

commented Feb 1, 2018

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

@Araq Araq closed this Feb 1, 2018

@GULPF

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

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...).

@Araq Araq removed the Language design label May 13, 2019

Araq added a commit that referenced this issue May 21, 2019

@Araq Araq closed this in 1e97b42 May 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.