Allow field replace to change type
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Mars |
Triaged
|
Wishlist
|
Matt Giuca |
Bug Description
Field replace is a syntactic sugar, but it currently has a semantic constraint that doesn't apply to the manually desugared version.
o.f := e is equivalent to:
C(o.0, o.1, ..., e, ..., o.n), where C is the constructor used by o, n is the number of fields in the constructor (minus one), and e is inserted at the index of f for that constructor.
The desugared version, however, allows the type to be changed, if the constructor is polymorphic, while the field-replace syntax does not. Consider:
type Pair(a, b):
Pair(fst :: a, snd :: b)
var x :: Pair(Int, Int)
var y :: Pair(Array(Int), Int)
x = Pair(1, 2)
y = x.fst := [3] # Type error: Found Array(Int), expected Int
It is plausible that the above code would work, and the expression "x.fst := [3]" would have type Pair(Array(Int), Int). Indeed, the desugared variant works as expected:
y = Pair([3], x.snd) # No type error