【发布时间】:2013-06-23 21:13:58
【问题描述】:
我正在尝试在 F# 中的类中创建公共方法。 C# 中的等价物是:
public void MyMethod<T>(string name, Thing<T> thingToProcess)
{
// Do stuff
}
在 F# 中,我正在尝试:
member public this.MyMethod<'T>((name : System.String), (thingToProcess : Thing<'T>)) =
(* Do similar stuff *)
()
此代码生成编译器错误:
错误 FS0670:此代码不够通用。类型变量 'T 不能是 泛化,因为它会超出其范围。
如果我尝试以下方法,我可以编译:
member public this.MyMethod((name : System.String), (thingToProcess : Thing<_>)) =
(* Some code *)
()
但是,尝试从 C# 调用该方法,如下所示失败:
public void DoSomething<T>(Thing<T> thingToProcess)
{
_instanceOfFSharpClass.MyMethod("A string", thingToProcess);
}
编译器错误:
'MyFSharpClass.MyMethod(string, Thing)' 的最佳重载方法匹配有 一些无效的参数。
建议?如何在 F# 中创建这种类型的方法?如果无法在 F# 中创建此方法,那么合理的解决方法是什么?如果可能的话,我需要避免将Thing<T> 转换为Thing<object>。
编辑:
这里有更多的 F# 代码。我会尽量坚持可能相关的部分。 EnumWithFlags 是来自带有 [FlagsAttribute] 的 C# 程序集的枚举。 cacheOne 以此处未列出的其他方法填充。 IInterface 在 C# 程序集中定义,只有一种方法,void ReceiveThing<T>(string name, Thing<T> thingToProcess)。函数TranslateThing 具有签名val TranslateThing : (Guid -> Thing<'T> -> TranslatedThing<'T>)。这有帮助吗?
type TranslatedThing<'T> =
| FirstThing of Thing<'T>
| SecondThing of Thing<System.String>
| ThirdThing of Thing<byte[]>
| FourthThing of Thing<System.String>
| IgnoreThing
[<AbstractClass>]
type public MyAbstractClass() =
let cacheOne = new ConcurrentDictionary<EnumWithFlags, Dictionary<Guid, IInterface>>()
member public this.MyMethod<'T>((name : System.String), (thingToProcess : Thing<'T>)) =
cacheOne.Keys.Where(fun key -> match key with
| k when (k &&& thingToProcess.EnumWithFlagsProperty) = EnumWithFlags.None -> false
| _ -> true)
.SelectMany(fun key -> cacheOne.[key].AsEnumerable())
.Distinct(
{
new IEqualityComparer<KeyValuePair<Guid, IInterface>> with
member x.Equals(a, b) = a.Key = b.Key
member x.GetHashCode y = y.Key.GetHashCode()
})
.AsParallel()
.Select(new Func<KeyValuePair<Guid, IInterface>, Tuple<IInterface, TranslatedThing<_>>>(fun kvp -> new Tuple<IInterface, TranslatedThing<'T>>(kvp.Value, TranslateThing kvp.Key thingToProcess)))
.Where(new Func<Tuple<IInterface, TranslatedThing<'T>>, bool>(fun t -> t.Item2 <> IgnoreThing))
.ForAll(new Action<Tuple<IInterface, TranslatedThing<'T>>>(fun t ->
match t.Item2 with
| FirstThing(x) -> t.Item1.ReceiveThing(name, x)
| SecondThing(x) -> t.Item1.ReceiveThing(name, x)
| ThirdThing(x) -> t.Item1.ReceiveThing(name, x)
| FourthThing(x) -> t.Item1.ReceiveThing(name, x)
| _ -> ()))
另一个修改:
经过多次提炼,我想我大致了解了导致问题的原因。我留下了MyMethod 的最后一行,因为取出它并没有解决错误。这一行是:
cacheTwo.Remove(thingToProcess) |> ignore
cacheTwo 在类的前面定义:
let cacheTwo = new Dictionary<Thing<'T>, SpecificThingTranslator<'T>>
SpecificThingTranslator<'T> 的签名在哪里:
type SpecificThingTranslator<'T> =
{First: TranslatedThing<'T>;
Second: Lazy<TranslatedThing<'T>>;
Third: Lazy<TranslatedThing<'T>>;
Fourth: Lazy<TranslatedThing<'T>>;}
删除cacheTwo 行并没有解决错误,因为函数TranslateThing 最终引用cacheTwo。消除对cacheTwo 的所有引用可以消除错误。
我可能会找到将Thing<'T> 映射到SpecificThingTranslator<'T> 的解决方法。不过,我在这里错过了什么吗?我是否忘记了允许此映射的 .NET 集合(或者可能是特定于 F# 的集合)?虽然每对的键和值的类型参数必须相同,但每个 KeyValuePair(或等效项)可以有不同的类型参数。
【问题讨论】:
-
一旦有明确的解决方案,我会在时间允许的情况下编辑问题以简洁明了。编译器错误对于 F# 新手来说有点神秘,而且 Google-the-error-message 方法没有产生任何似乎适用的结果。我希望其他遇到错误信息的程序员可以从这个问答中受益。