SciNim / Unchained

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

`const` variables lose their type information #5

Closed Vindaar closed 2 years ago

Vindaar commented 3 years ago

I've stumbled on this before, but ignored it, because I didn't have the time to isolate it and provide a Nim issue about it. But now I got caught by this bug again, so it's time to at least file an issue here.

If one defines a variable as const that has an associated type, the type information will be lost by the time this variable is used in some computation. If the type contains SI prefixes this is especially dangerous, because one misses conversion factors. Raw type information is a (comparatively) minor issue, because it would at least show up as a CT error due to missing types (which still might happen of course, but in cases where the types match regardless (i.e. just a computation and manual conversion to float) the numbers are plain wrong.

const g_aγ = 1e-10.GeV⁻¹
let x = g_aγ * 1.0.eV²

will result in x of type eV² and the conversion factor of 1e-9 missing from the result.

Vindaar commented 2 years ago

Closed by #25.