You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The normal semantics for var t T in an ex rule is that t will match any expression whose type is assignable to T. In the example below, i and v are both assignable to I, which implements interface { Equal(I) bool }, so I would expect all of the equals to be rewritten. However, currently only the first gets rewritten:
ex {
type T interface { Equal(T) bool }
var t1, t2 T
t1 == t2 -> t1.Equal(t2)
}
-- x.go --
package p
type I interface { Equal(I) bool }
var i I
type V int
func (V) Equal(I) bool
var v V
var _ I = v
var _ = i == i
var _ = i == v
var _ = v == i
var _ = v == v
I suspect matcher.matchWildcardType needs to be extended to support type unification rather than requiring strict type identity for back matches.
There's probably some subtlety here for preferring that the unification to prefer unifying to I rather than interface { Equal(I) bool }. Might want to learn from how types2 handles this.
In the mean time, it's possible to manually workaround this without too many additional rules:
ex {
type A interface{ Equal(A) bool }
type B interface{ Equal(A) bool }
var a1, a2 A
var b1, b2 B
a1 == a2 -> a1.Equal(a2)
a1 == b2 -> a1.Equal(b2)
b1 == a2 -> b1.Equal(a2)
b1 == b2 -> b1.Equal(b2)
}
-- x.go --
package p
type I interface{ Equal(I) bool }
var i I
type V int
func (V) Equal(I) bool
var v V
var _ I = v
var _ = i == i
var _ = i == v
var _ = v == i
var _ = v == v
The text was updated successfully, but these errors were encountered:
mdempsky
changed the title
rf: type inference for type patterns
rf: type unification for type patterns
Dec 7, 2020
The normal semantics for
var t T
in an ex rule is thatt
will match any expression whose type is assignable toT
. In the example below,i
andv
are both assignable toI
, which implementsinterface { Equal(I) bool }
, so I would expect all of the equals to be rewritten. However, currently only the first gets rewritten:I suspect
matcher.matchWildcardType
needs to be extended to support type unification rather than requiring strict type identity for back matches.There's probably some subtlety here for preferring that the unification to prefer unifying to
I
rather thaninterface { Equal(I) bool }
. Might want to learn from how types2 handles this.In the mean time, it's possible to manually workaround this without too many additional rules:
The text was updated successfully, but these errors were encountered: