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
This code has an undesirable property: Since T_in is invariant, produce_from_value can only be called on instances of Container whose type parameter is exactlyCanProduce[U], for some U:
classIntProducer(CanProduce[int]):
defproduce(self) ->int:
return42c: Container[IntProducer] =Container(IntProducer())
c.produce_from_value() # this produces a type error
It would be ideal if we could communicate that in this method (which need not be an instance method, it could be a discrete function also), the type parameter behaves as though it were covariant, where any subtype of CanProduce[U] is valid.
In fact, there exists a mechanism for doing this in other situations. Consider if we have the following non-generic protocol:
Suppose we have the following code:
This code has an undesirable property: Since
T_in
is invariant,produce_from_value
can only be called on instances ofContainer
whose type parameter is exactlyCanProduce[U]
, for someU
:It would be ideal if we could communicate that in this method (which need not be an instance method, it could be a discrete function also), the type parameter behaves as though it were covariant, where any subtype of
CanProduce[U]
is valid.In fact, there exists a mechanism for doing this in other situations. Consider if we have the following non-generic protocol:
We can then make a method
index_from_value
that works with all subtypes ofSupportsIndex
:Now, the following is valid:
Unfortunately, this method cannot be used with generic protocols, because the following is invalid:
I suggest altering the restriction to allow this trick to work.
The text was updated successfully, but these errors were encountered: