By the way, to restrict conversion to literals you can use the {lit} constraint
converter toIntAp(n: int{lit}): IntAp
Run
Otherwise some subtle bugs might be introduced where you or your user do not
want because an int was converted silently to IntAp.
Wow thanks, I didn't know about this use of converters. I can simplify a lot it
code with this!
No need apologize! Holy cow ... how did I not know about `converter`? I've been
coding Nim for over a year now and I just now learned of it.
I will have to re-read the manuals again. I wonder if there are any other
hidden nuggets I've missed.
Overloading of `=` is in devel but the signature always must be `= (dest: var
T; source: T)` and it cannot be used to introduce new implicit conversions, for
that you have to use a `converter`. There are currently no plans to change
this, sorry.
Perhaps like this
type IntAp = object
setInts: array[0 .. 2, int]
converter toIntAp(n: int): IntAp =
let ints = [n, 0, 0]
IntAp(setInts: ints)
let n: IntAp = 5
Run
If, for example, I were wanting to create a crazy new int type that stored its
value as product of primes, and I was not concerned with performance, I could
create my own pure nim type in a library.
type
intAP = object
setOfPrimes: array[0..50, int]
Run