FsCheck icon indicating copy to clipboard operation
FsCheck copied to clipboard

Gen.OneOf contravariance (with interface)

Open sm-g opened this issue 8 years ago • 4 comments

I want to write such code:

from data in Gen.OneOf<ScrapperOutput.IData>(FailDataGenerator(),ExDataGenerator(), RatesDataGenerator())

where each of DataGenerators return specific types. Currently this problem solved by casting result from Generators to interface:

 public static Gen<ScrapperOutput.IData> ExDataGenerator()
        {
            return from ex in Arb.Generate<ArgumentException>()
                   select new ExData(ex) as ScrapperOutput.IData;
        }

sm-g avatar Aug 03 '16 06:08 sm-g

Yes, interesting suggestion. Unfortunately F# cannot even declare variant interfaces so far, so wouldn't really know how to go about it (except by having a separate FsCheckCS dll which seems a bit overkill for this one feature. Suggestions?

kurtschelfthout avatar Aug 03 '16 20:08 kurtschelfthout

@kurtschelfthout I think we could check the variance by checking manually whether the type is derived from or not derived recursively. But this only applies for covariance only, For contravariance, I could not find nice ways to do it.

eriawan avatar Sep 08 '16 03:09 eriawan

@eriawan I don't quite understand what you mean with checking manually - afaik this is something that the compiler should support? I mean even if the F# compiler doesn't recognize variance, if it could allow us to give variance annotations that can be seen by C#/VB.NET that would help.

kurtschelfthout avatar Sep 09 '16 19:09 kurtschelfthout

@kurtschelfthout by checking manually I mean traversing back the inheritance from the type to the parent type, and then back to the desired parent type recursively.

A sample case of this is available on this stackoverflow question: http://stackoverflow.com/questions/5368655/how-to-check-in-f-whether-object-implements-interface

I know it's better to have build-in support for covariance and contravariance in F#. I have proposed this feature (on March 2014) in F# uservoice: https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663470-provide-covariance-contravariance-language-support

The current status is still UNDER REVIEW but I hope they will make a concrete implementation after F# 4.1 is done released.

AFAIK, to implement covariance-contravariance on a language that has implementation on Hindley-Milner type inference such as F# and Haskell is harder (could be more than twice difficult) than implementing on a language that has relaxed type inference such as C# and VB (and in VB is somehow easier than C# because VB has support for late bound since its first release).

eriawan avatar Sep 10 '16 06:09 eriawan

Doesn't look like this is ever going to happen on the F# side, so closing as not planned.

kurtschelfthout avatar Feb 25 '24 19:02 kurtschelfthout