Generics F#等价于公共C#泛型方法
我正在尝试在F#中的类中创建公共方法。C#中的等效值为:Generics F#等价于公共C#泛型方法,generics,f#,c#-to-f#,Generics,F#,C# To F#,我正在尝试在F#中的类中创建公共方法。C#中的等效值为: public void MyMethod<T>(string name, Thing<T> thingToProcess) { // Do stuff } 另一次编辑: 经过多次提炼,我想我大致了解了问题的起因。我省略了MyMethod的最后一行,因为去掉它并不能解决错误。这一行是: cacheTwo.Remove(thingToProcess) |> ignore 其中,cacheTwo在类的前
public void MyMethod<T>(string name, Thing<T> thingToProcess)
{
// Do stuff
}
另一次编辑:
经过多次提炼,我想我大致了解了问题的起因。我省略了MyMethod
的最后一行,因为去掉它并不能解决错误。这一行是:
cacheTwo.Remove(thingToProcess) |> ignore
其中,cacheTwo
在类的前面定义:
let cacheTwo = new Dictionary<Thing<'T>, SpecificThingTranslator<'T>>
let cacheTwo=new Dictionary>
其中SpecificThingTranslator的签名=
{第一:翻译内容>;
第三:懒惰>;}
消除cacheTwo
行并不能解决错误,因为函数TranslateThing
最终引用了cacheTwo
。消除对cacheTwo
的所有引用可消除该错误
我可能可以找到一个解决方法来映射
事物
。然而,我是否错过了什么?我是否忘记了允许这种映射的.NET集合(或者特定于F的集合)?虽然每对密钥和值的类型参数必须相同,但每对KeyValuePair(或等效项)可以有不同的类型参数。代码的另一部分肯定有问题。以下最小示例具有与MyMethod
完全相同的定义,并且工作正常(粘贴到新脚本文件时):
类型(东西)=
()
我删除了
public
修饰符,因为这是F#中成员的默认值,我还删除了其他括号,但除此之外,没有任何更改….您的代码的另一部分肯定有问题。以下最小示例具有与MyMethod
完全相同的定义,并且工作正常(粘贴到新脚本文件时):
类型(东西)=
()
我删除了
public
修饰符,因为这是F#中成员的默认值,我还删除了附加的括号,但除此之外,很可能没有任何变化……。我会补充一些细节,看看这是否有用。是的,如果你能给出一个给出错误的最小样本,那就有可能给出一些更有用的答案!非常感谢。我已经添加了代码,不管它有多不雅。正如您可能猜到的,我对F#相当陌生。@Andrew即使有了更新的版本,我仍然无法复制错误(我只是添加了所有必要的定义以使您的代码能够编译…),因此,您可能也需要添加多一点(或简化代码以找到问题的根源…)再次感谢您的尝试。今晚我将尝试提取这段代码。很可能。我会补充一些细节,看看这是否有用。是的,如果你能给出一个给出错误的最小样本,那就有可能给出一些更有用的答案!非常感谢。我已经添加了代码,不管它有多不雅。正如您可能猜到的,我对F#相当陌生。@Andrew即使有了更新的版本,我仍然无法复制错误(我只是添加了所有必要的定义以使您的代码能够编译…),因此,您可能也需要添加多一点(或简化代码以找到问题的根源…)再次感谢您的尝试。今晚我将尝试提炼这段代码。如果时间允许,一旦有了明确的解决方案,我将对问题进行编辑,使其简洁明了。编译器错误对于一个F#新手来说有点神秘,而Google的错误消息方法并没有产生任何似乎适用的结果。我希望另一个遇到错误消息的程序员能够从这个问题和答案中受益。如果时间允许,一旦有了明确的解决方案,我将编辑这个问题,使其简洁明了。编译器错误对于一个F#新手来说有点神秘,而Google的错误消息方法并没有产生任何似乎适用的结果。我希望另一个遇到错误消息的程序员能够从这个问题和答案中获益。
public void DoSomething<T>(Thing<T> thingToProcess)
{
_instanceOfFSharpClass.MyMethod("A string", thingToProcess);
}
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)
| _ -> ()))
cacheTwo.Remove(thingToProcess) |> ignore
let cacheTwo = new Dictionary<Thing<'T>, SpecificThingTranslator<'T>>
type SpecificThingTranslator<'T> =
{First: TranslatedThing<'T>;
Second: Lazy<TranslatedThing<'T>>;
Third: Lazy<TranslatedThing<'T>>;
Fourth: Lazy<TranslatedThing<'T>>;}
type Thing<'T> = T of 'T
type Foo() =
member this.MyMethod<'T>(name:string, thingToProcess:Thing<'T>) =
()