提问人:Alan Wayne 提问时间:8/9/2021 更新时间:8/9/2021 访问量:112
如何在 F# 中定义和实现返回非类型化 IEnumerable 的接口方法?
How to define and implement an interface method in F# that returns an untyped IEnumerable?
问:
在 C# 中,我有以下接口定义:
using System.Collections;
public interface ISuggestionProvider
{
#region Public Methods
IEnumerable GetSuggestions(string filter);
#endregion Public Methods
}
在 F# 中,我尝试过这样做:
type ISuggestionProvider =
abstract member GetSuggestions: string -> seq<'T>
type DiagnosisProvider () =
interface ISuggestionProvider with
member this.GetSuggestions s =[ "one"; "two"; "three"] |> Seq.cast
但是当它读回 C# 时,我得到:
public IEnumerable<T> GetSuggestions<T>(string value)
{
throw new NotImplementedException();
}
我需要的是将其读回:
public System.Collections.IEnumerable GetSuggestions(string filter)
{
return _method(filter);
}
简而言之,如何返回非类型的 IEnumerable 而不是 IEnumerable<'T> ???
提前感谢您的任何帮助。
答:
8赞
Phillip Carter
8/9/2021
#1
您需要根据 来定义接口的实现,而不是 。F# 序列是泛型序列,因此,如果需要非泛型序列,则不能使用序列。IEnumerable
seq<'T>
open System.Collections
type ISuggestionProvider =
abstract member GetSuggestions: string -> IEnumerable
type DiagnosisProvider () =
interface ISuggestionProvider with
member this.GetSuggestions s =
[ "one"; "two"; "three"]
:> IEnumerable
下一个:F# seq 行为
评论