Your are right! The assembler code generate by c compiler with simple object
wrapper is almost same with the plain int.
It is `distinct int` and `float`, not with `int`.
`distinct` would perform the same as an object/tuple with a single element if
you compile with optimizations. Convertsions between `distinct T` and `T` are
fundamental, I don't see why would you want to disallow them.
Use `distinct` for performance, disable `Price(3.6)` for safety.
I'm not sure if what you want is achievable with `distinct`, but as a
workaround you can just make it a normal `object`, which would be better since
you don't want to allow your user to directly convert from/to Price and int.
I hope I can find way to prevent the code like this:
proc Price(x: float) {.error.}
Run
But this doesn't compile.
But this code compiles fine.
type Price = distinct int
var f = 3.6
var p = Price(f)
echo p # 3, rather than 4
Run
This is what I want to avoid.
There is no need to disable the conversion as it's only done for _literals_
anyway. This means that this does **not** compile:
proc takesFloat(x: float) = discard
var i = 44
takesFloat(i)
Run
Look at the following code.
type Price = distinct int
converter float_to_price(x: float): Price = x.toInt.Price
let x = 3.6
let y = Price(x) # y = 3, I want to disable it
let z: Price = x # z = 4, correct
Run