nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.44k stars 1.47k forks source link

Equality operator throws error of type mismatch but not when it's used only once. #13785

Open zetashift opened 4 years ago

zetashift commented 4 years ago

See the playground example, if I have matrix3x3 and matrix4x4 defined, Nim throws an error of type mismatch at me, however if one of them is commented out, everything is fine and I get a successful compilation.

Example

Failing example: https://play.nim-lang.org/#ix=2gv9 Working example: https://play.nim-lang.org/#ix=2fGO

Current Output

/playground/nim/lib/pure/unittest.nim(665, 14) Error: type mismatch: got <Matrix[W - 1, H - 1], Matrix[3, 3]>
but expected one of: 
func `==`[W, H](m1, m2: Matrix[W, H]): bool
  first type mismatch at position: 2
  required type for m2: Matrix[==.W, ==.H]
  but expression ':c2' is of type: Matrix[3, 3]
...

Expected Output

All the tests passing

Possible Solution

None found, sorry :(.

Additional Information

I have not found a solution, replacing the suite/tests with a block statement and just assert expressions resulted in the same error. So it has nothing to do with unittest.

$ nim -v
1.0.6 and 1.1.1
febbow commented 4 years ago

if it helps, an initial guess: seems like a typo in Line 19, see comment # below """ func ==*[W,H](m1, m2: Matrix[W, H]): bool = result = true for x in 0 .. high(m1): for y in 0 .. high(m1): #why not high(m1[0])? if not m1[x][y].equal(m2[x][y]): return false """

zetashift commented 4 years ago

if it helps, an initial guess: seems like a typo in Line 19, see comment # below """ func ==*[W,H](m1, m2: Matrix[W, H]): bool = result = true for x in 0 .. high(m1): for y in 0 .. high(m1): #why not high(m1[0])? if not m1[x][y].equal(m2[x][y]): return false """

Good catch, this is my bad! But since the matrix are equal sizes(2x2, 3x3 or 4x4) that iteration doesn't matter. I think I caught this and left it like that when I initially wrote the function.

Even changing that I still get the same error.

mratsim commented 4 years ago

It works?

image

zetashift commented 4 years ago

@mratsim I think I should've made the default code be the failing case. :P if you see in the setup block that you should comment out the code to let it error out; like this: https://play.nim-lang.org/#ix=2gv9

For the current one you need to uncomment the matrix3x3 definition and it's test block.

Araq commented 4 years ago

Lol, @mratsim I made the same mistake...