SciNim / Unchained

A fully type safe, compile time only units library.
https://scinim.github.io/Unchained
110 stars 0 forks source link

Can't use two generic parameters of the same quantity #47

Closed arkanoid87 closed 8 months ago

arkanoid87 commented 8 months ago
import unchained

type Foo[DA: Length, DB: Length] = object
  a: DA
  b: DB

proc initFoo[DA: Length, DB: Length](a: DA, b: DB): Foo[DA, DB] =
  Foo[DA, DB](a: a, b: b)

echo initFoo(1.m, 2.m) # compiles correctly
echo initFoo(1.m, 2.cm) # got: <typedesc[Meter], typedesc[CentiMeter]> but expected: <DA: Length, DB: Length>

I've tried a couple of other quantities, and I got same error, but can't say if it happens for all quantities.

Vindaar commented 8 months ago

It's probably a bug in the compiler regarding old style concepts. The type definition is the issue. The compiler seems to bind the Length in the Foo definition to the same symbols. The following variant:

import unchained

type Foo[DA, DB] = object
  a: DA
  b: DB

proc initFoo[DA: Length, DB: Length](a: DA, b: DB): Foo[DA, DB] =
  Foo[DA, DB](a: a, b: b)

echo initFoo(1.m, 2.m) # compiles correctly
echo initFoo(1.m, 2.cm) # got: <typedesc[Meter], typedesc[CentiMeter]> but expected: <DA: Length, DB: Length>

works fine.

Feel free to report it as a Nim issue. This code here reproduces the same problem:

type
  cm = distinct float
  m = distinct float
  Length = concept x
    x.float is float

type Foo[DA: Length, DB: Length] = object
  a: DA
  b: DB

proc initFoo[DA: Length, DB: Length](a: DA, b: DB): Foo[DA, DB] =
  Foo[DA, DB](a: a, b: b)

echo initFoo(1.m, 2.m) # compiles correctly
echo initFoo(1.m, 2.cm) # got: <typedesc[Meter], typedesc[CentiMeter]> but expected: <DA: Length, DB: Length>
arkanoid87 commented 8 months ago

Thanks a lot for the quick and complete response.

arkanoid87 commented 8 months ago

here we go https://github.com/nim-lang/Nim/issues/23405