简体   繁体   中英

Equivalent to F# _ wildcard in C# type parameter

F# allows you to let the compiler work out what type paramters should be.

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?

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).

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.

(I understand why C#'s inference is weaker, it's life, but my examples are actually pretty mechanical)

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.

The cost is having an F# wrapper of an external F# library, but the code is trivial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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