How to define and implement an interface method in F# that returns an untyped IEnumerable?

Viewed 85

In C#, I have the following interface definition:

using System.Collections;

public interface ISuggestionProvider
{

    #region Public Methods

    IEnumerable GetSuggestions(string filter);

    #endregion Public Methods

}

In F#, I have tried doing this:

type ISuggestionProvider = 
    abstract member GetSuggestions: string -> seq<'T>

type DiagnosisProvider () = 
   interface ISuggestionProvider with
    member this.GetSuggestions s =[ "one";  "two"; "three"] |> Seq.cast

But when this is read back into C#, I get:

public IEnumerable<T> GetSuggestions<T>(string value)
 {
        throw new NotImplementedException();
 }

What I need is it to be read back as:

    public System.Collections.IEnumerable GetSuggestions(string filter)
    {
        return _method(filter);
    }

In short, how do I return an untyped IEnumerable instead of IEnumerable<'T> ???

Thanks in advance for any help.

1 Answers

You need to define your interface an implementation in terms of IEnumerable, not seq<'T>. F# sequences are generic, so if you need the non-generic one you can't use sequences.

open System.Collections

type ISuggestionProvider = 
    abstract member GetSuggestions: string -> IEnumerable

type DiagnosisProvider () = 
   interface ISuggestionProvider with
      member this.GetSuggestions s =
          [ "one";  "two"; "three"]
          :> IEnumerable
Related