Implicit Conversion of Collections and Observables

I have a question

When A implicitly converts to B, when should Collection<A> also implicitly convert to Collection<B>?

Same question for Observables

The answer at the moment seems inconsistent, and different also for implicit upcasting vs implicit numeric conversions.

Why is this? How could the general rule be stated? (Or would you just say “it’s arbitrary and if it implicit conversion of a collection doesn’t work you have to do implicit conversion in the singular context e.g. with a ForEach loop”)

Collection<Int32> to Collection<Float32> actually never works. It only appears to work in a few cases here due to singular conversions that are happening instead.

ImplicitConversionQuestion.vl (79.0 KB)

I think I maybe found some answers

Why does Collection<Subtype> to Collection<Supertype> work, but Collection<float> to Collection<integer> does not?

“Variance is supported only if a type parameter is a reference type. Variance is not supported for value types.”

From here

Why do IEnumerable and IObservable support this kind of conversion?
both their documentation pages state
“This [generic] type parameter is covariant. That is, you can use either the type you specified or any type that is more derived.”

Which only leaves this question
Why does Spread<T> not support covariance if it implements IEnumerable?

“This feature works only for generic interfaces and delegates. If you implement a variant generic interface, the implementing class is still invariant.”

From here

Can any dev confirm that’s right?

Yes that’s correct.

1 Like