16412, “vasslitvinov”, “CG: implicit coercion”, “2020-09-18T01:25:53Z”
Main issue: #8629
How does CG (constrained generics) design support coercions?
Consider three types A
, B
, C
, such that there is an implicit coercion from A
to B
and from B
to C
. In my code:
-
The interface requires
proc foo(x: B)
. -
Within the CG function, I call
foo(arg)
wherearg
has the typeA
. -
The interface is implemented with
proc foo(x: C)
.
Should this be allowed? This sounds natural.
Cons: we currently do not allow transitive implicit coercion. If an implicit coercion from A
to C
is not defined, it is invalid to invoke proc foo(x:C)
on arg:A
. The above scenario would bypass this restriction.
One solution could be to allow only one coercion for any given argument. In the above scenario, either the call within the CG function would have to cast expicitly arg:B
or the interface would have to be implemented with proc foo(x:B)
, not both.