@Araq What Nim version? It fails with "object constructor needs an object type"
on my fresh-downloaded Nim 1.17.0...
By the way, it is counter-intuitive, I guess. It wouldn't be so if the syntax
was available for any type, e.g. arrays. It's a bit misleading as casts can
look a bit similar to constructors:
type
Modular[N: static[Positive]] = distinct range[0..N]
ModularSet[N: static[Positive]] = distinct array[N, bool]
let mr1 = Modular[3](2) # cast
let ms1 = ModularSet[3]([true, false, true]) #cast
let mr2 = Modular[17]() # constructor, error: object constructor needs an
object type
let ms2 = ModularSet[17]() # constructor, error: object constructor needs
an object type
Please note the warnings about initialization (can't Nim prove 3 or 17 are
Positive?).
Here, of course, one can just add type declarations without initialization
but...
let mr1 = Modular[4](2)
let mr2 = Modular[4](3)
let ms = ModularSet[4]([true, false, false, true])
proc contains[N: static[Positive]]
(set: ModularSet[N], num: Modular[N]): bool = ...
echo (mr1 in ms) # false
echo (mr2 in ms) # true
echo (mr1 in ModularSet[7]()) # fails
Firstly: it fails for N: static[Positive], we need to use N: static[int]. Once
again: can't Nim infer 4 is Positive?
Secondly: the last one would actually work for an object.