Brian's solution looks great to me, but do you really need array covariance?
If you have a function that accepts ISomething[]
and wants to pass SomeSomething[]
, then you will need it, but if the function only reads values โโof the ISomething
type from the array (which allows covariance) then you can use the hash type and write a function, which accepts #ISomething[]
. (Assuming you can change the function, of course!)
Thus, the function can work with an array of any elements that implement ISomething
, and you do not need the covariance of the array when called. Here is an example:
type A() = interface IDisposable with member x.Dispose() = printfn "gone" // Disposes the first element from an array let disposeFirst (a:
It seems to me that the main reason for array covariance in .NET is that it was needed at a time when generics did not exist, but it seems that F # can live without this function just fine.
source share