3
votes

I have the below code:

type IQuery = 
    abstract List<'T> : unit -> IList<'T>


let create (str)=
   let getList () : IList<'T> = upcast List<'T>()
   { new IQuery with
      member this.List<'T>() = getList<'T>()

And for the last line it gives me a warning stating that:

The method or function 'getList' should not be given explicit type argument(s) because it does not declare its type parameters explicitly

However if I remove <'T> from getList call then I get a compilation error as :

The member 'List<'T> : unit -> IList<'a>' does not have the correct type to override the corresponding abstract method. The required signature is 'List<'T> : unit -> IList<'T>'.

What can I do ?

1

1 Answers

2
votes

You can declare getList with an explicit type parameter:

let getList<'T> () : IList<'T> = upcast List<'T>()

You then get an error:

Explicit type parameters may only be used on module or member bindings

If you then move the let binding to the top-level at the same scope as the type, it all works:

type IQuery = 
    abstract List<'T> : unit -> IList<'T>

let getList<'T> () : IList<'T> = upcast List<'T>()

let create (str) =
       { new IQuery with
          member this.List<'T>() = getList<'T>()
       }

If your real code has getList using values only in scope in create, like str, you'll need to add them as explicit parameters to getList.