简体   繁体   中英

Bug in F# 2.0 wrt to where constraints in generic type parameters?

If I have a C# method that I want to consume from F# and receives two typed parameters like this:

public class Foo
{
    public static void GenericMethodWithTwoTypeParamsThatHaveAWhereClass<TA, TB>() where TA : TB
    {

    }
}

When I try to call it via F#, the compiler complains:

Foo.GenericMethodWithTwoTypeParamsThatHaveAWhereClass<System.IO.BinaryWriter, System.IDisposable>()

So, is this a bug in F# 2.0?

This type of constraint is not supported in F#. The spec indicates constraints of the form t1 :> t2 are treated as t1 = t2 , which explains your error:

This expression was expected to have type BinaryWriter but here has type System.IDisposable

Depending how TB is used, you might be able to get by with

GenericMethodWithTwoTypeParamsThatHaveAWhereClass<BinaryWriter, _>()

TB will be inferred as BinaryWriter , according to the aforementioned rule. However, if that works then your C# method can likely do with one type param.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM