Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer generic types of implemented interfaces #817

CameronAavik opened this issue Dec 8, 2019 · 1 comment

Infer generic types of implemented interfaces #817

CameronAavik opened this issue Dec 8, 2019 · 1 comment


Copy link

@CameronAavik CameronAavik commented Dec 8, 2019

Infer generic types of implemented interfaces

I propose we add support to infer the generic types of an implemented interface. At the moment, the following code fails to compile:

type Factory<'T> =
    abstract member Create : unit -> 'T

type ZeroFactory =
    interface Factory<_> with
        override _.Create () = 0

FS0909 All implemented interfaces should be declared on the initial declaration of the type
FS0715 Anonymous type variables are not permitted in this declaration

Changing Factory<_> to Factory<int> fixes the errors. However, given the implementation of the interface, I think F# should be able to infer that the type is an int.

It also seems that object expressions do support inferring this generic type and is able to compile the following successfully.

// type of zeroFactory is inferred to be Factory<int>
let zeroFactory = 
    { new Factory<_> with
        override _.Create () = 0 }

The existing way of approaching this problem in F# is to make the types explicit or to use an object expression as above.

Pros and Cons

The advantage of making this adjustment to F# is having another place where F# can help infer types for us without making it explicit.

The disadvantages of making this adjustment to F# are unknown to me. There may be reasons why this isn't simple to infer that I don't know about.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions:

  • [Umbrella] improve Type Inference #594

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

This comment has been minimized.

Copy link

@cartermp cartermp commented Dec 8, 2019

From a consistency standpoint I think this is reasonable. I would expect inference to be the same in both an object expression and an implementation on a type.

BTW the sample doesn't require overriding. The following is more standard:

type Factory<'T> =
    abstract Create : unit -> 'T

type ZeroFactory =
    interface Factory<_> with
        member _.Create () = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.