简体   繁体   English

相当于C#类型参数中的F#_通配符

[英]Equivalent to F# _ wildcard in C# type parameter

F# allows you to let the compiler work out what type paramters should be. F# 允许您让编译器计算出应该是什么类型的参数。

let foo = [| 1 |]
let bar = Array.head<_> foo

(you'd never actually do this, you'd drop the parameter, but this is just illustrative). (您实际上永远不会这样做,您会删除参数,但这只是说明性的)。

Is there something in C# that's equivalent? C# 中是否有等价的东西?

The problem is...I have F# code that takes literally 30 type parameters, calling this code from F# is trivial, the compiler just works it out (and if it couldn't, then specifying 1 parameter and wildcarding the rest would nicely hint to the compiler what to do), calling this code from C# is horrific, as you have to painstakingly work out the parameters (if you leave them out completely, it stubbornly refuses to infer them).问题是……我有 F# 代码,它实际上需要 30 个类型参数,从 F# 调用这段代码很简单,编译器只是解决了它(如果不能,那么指定 1 个参数并通配其余部分会很好地提示编译器该怎么做),从 C# 调用这段代码是可怕的,因为你必须煞费苦心地计算出参数(如果你完全不考虑它们,它会顽固地拒绝推断它们)。

PS聚苯乙烯

even nasty hacks like getting the IDE to explicitly specify these parameter in F# code, that can then be largely pasted into the C# code would help.即使是让 IDE 在 F# 代码中显式指定这些参数的讨厌的黑客,然后可以在很大程度上粘贴到 C# 代码中也会有所帮助。

(I understand why C#'s inference is weaker, it's life, but my examples are actually pretty mechanical) (我明白为什么 C# 的推理较弱,这就是生活,但我的例子实际上很机械)

Actually I've solved it.其实我已经解决了。

If I wrap the ultra generic function with a call from the specific types AND WRITE THAT IN F#, I can then call this function, which isn't type parametric at all, from my C# code, and not spend 15 minutes trying to work out what the types are.如果我用来自特定类型的调用包装超通用函数并在 F# 中写入,然后我可以从我的 C# 代码调用这个函数,它根本不是类型参数,而不是花 15 分钟来尝试解决类型是什么。

The cost is having an F# wrapper of an external F# library, but the code is trivial.代价是使用外部 F# 库的 F# 包装器,但代码很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM