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

Different type inferred when setting a default value for an array field #22926

Closed
ericvw opened this issue Nov 9, 2023 · 2 comments · Fixed by #22999
Closed

Different type inferred when setting a default value for an array field #22926

ericvw opened this issue Nov 9, 2023 · 2 comments · Fixed by #22999
Assignees

Comments

@ericvw
Copy link
Contributor

ericvw commented Nov 9, 2023

Description

When setting a default value for an array field in an object where the size is as an enumeration type, attempting to iterate over the array in a for-loop yields a compiler error because it infers the type as being different than the declared type.

The code below illustrates the issue:

type
    Direction = enum
        North
        South
        East
        West

    ArrayObj1 = object
        list: array[Direction, int]

    ArrayObj2 = object
        list: array[Direction, int] = [1, 2, 3, 4]


var a: ArrayObj1
for i in Direction:
    echo a.list[i] # Compiles without issue.

var b: ArrayObj2
for i in Direction:
    echo b.list[i] # Errors that there is a type mismatch.

Nim Version

Nim Compiler Version 2.0.0 [MacOSX: amd64]
Compiled at 2023-10-05
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

Current Output

$ nim c test.nim
......................................................................
/Users/eric/projects/advent-of-code.git/2022/test.nim(21, 16) Error: type mismatch
Expression: `[]`(b.list, i)
  [1] b.list: array[0..3, int]
  [2] i: Direction

Expected one of (first mismatch at [position]):
[0] proc `[]`(s: string; i: BackwardsIndex): char
[0] proc `[]`(s: var string; i: BackwardsIndex): var char
[0] proc `[]`[I: Ordinal; T](a: T; i: I): T
[0] proc `[]`[Idx, T; U, V: Ordinal](a: array[Idx, T]; x: HSlice[U, V]): seq[T]
[0] proc `[]`[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T
[0] proc `[]`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T
[0] proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string
[0] proc `[]`[T; U, V: Ordinal](s: openArray[T]; x: HSlice[U, V]): seq[T]
[0] proc `[]`[T](s: openArray[T]; i: BackwardsIndex): T
[0] proc `[]`[T](s: var openArray[T]; i: BackwardsIndex): var T
[0] template `[]`(a: WideCStringObj; idx: int): Utf16Char
[0] template `[]`(s: string; i: int): char

Expected Output

The program to compile without error.

Possible Solution

No response

Additional Information

No response

@beef331
Copy link
Collaborator

beef331 commented Nov 9, 2023

Just for a workaround:

type
    Direction = enum
        North
        South
        East
        West

    ArrayObj1 = object
        list: array[Direction, int]

    ArrayObj2 = object
        list: array[Direction, int] = [North: 1, 2, 3, 4]


var a: ArrayObj1
for i in Direction:
    echo a.list[i]

var b: ArrayObj2
for i in Direction:
    echo b.list[i]

@ericvw
Copy link
Contributor Author

ericvw commented Nov 9, 2023

Thanks for the workaround, @beef331. I'll use that for the time being.

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