Open arnetheduck opened 4 years ago
This works:
type
Obj = object
v: int
HasFrm = object
v: int
template frm[T: HasFrm](Te: typedesc[T], o: Obj): T = Te(v: o.v)
template to*[T](v: auto, Te: typedesc[T]): T =
mixin frm
frm(Te, v)
let
o = Obj(v: 42)
hft = o.to(HasFrm)
typedesc[T]
and type[T]
work but type T
breaks. This is one bug.
The bug in the original example has to do with the fact that we are using the template parameter T
in the signature (which has constraint None
, and so this is what the return type is inferred as early on). Normally procs would bind the type of T to the implicit generic it's attached to which would give the correct return type on instantiation, but I'm not sure if implicit generics exist in templates. If they don't, we can force the use of generics here and disallow using template parameters in the signature, or at least suggest using generics.
See also #7160
Example
Current Output