Generic index into two types not seen as assignable, even if the two types are identical for all relevant keys #58905
Labels
Design Limitation
Constraints of the existing architecture prevent this from being fixed
π Search Terms
generic, indexed access, supertype
π Version & Regression Information
β― Playground Link
Playground link
π» Code
π Actual behavior
bar(k, v)
fails becauseFoo[K]
is supposedly not assignable toBar[K]
.π Expected behavior
bar(k, v)
should succeed becauseFoo[K]
is identical toBar[K]
; the only difference betweenFoo
andBar
occurs in a key thatK
can never be.Additional information about the issue
This is presumably a design limitation introduced with #30769. And while it's similar to #32693, it's different enough that I'd like to see an official word about it. If there is an existing issue that's the same as this, I didn't find it. Can anyone find one this duplicates? I'm investigating a SO question and while I feel certain I've seen this before, I keep hitting things like #32693 which aren't about generics at all.
On the face of it, if
Pick<Foo, KC>
andPick<Bar, KC>
are identical, then for genericK extends KC
,Foo[K]
andBar[K]
should also be identical. Yes, ifK
turns out to be a union, then you run into the soundness issue that #30769 fixed. We already allowT[K]
to be assignable toT[K]
ifK
is generic, so it's not really soundness that's at play here.... we've already decided that generic keys are a way of sidestepping this (I'm looking at you, #47109). I'm guessing it's just not worth the performance hit to check assignability of parts of the source and target object types, so it's a design limitation? Maybe?The text was updated successfully, but these errors were encountered: