问题
I've noticed difficulty in piping to/from some .NET methods. Toy example
let foo = System.String [| 'a'; 'b'; 'c' |] // works
let foo = [| 'a'; 'b'; 'c' |] |> System.String // fails
// error FS0802: Invalid use of a type name and/or object constructor.
let foo = System.String <| [| 'a'; 'b'; 'c' |] // fails the same way
let foo = [| 'a'; 'b'; 'c' |] |> new System.String // Fails
// error FS0010: Incomplete structured construct at or before this point in expression
I'm basically trying to figure out when you can combine piping with .NET objects and when not. If there's a reference out there I'd love to get the link!
回答1:
As for the hang-up you're having with strings, the following link shows that support for treating constructors as functions was added to F# 4.0
https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663317-allow-to-use-class-constructors-as-functions
Another common situation that makes piping awkward from .NET libraries is that they're exposed as tupled
(as opposed to curried
) function parameters, which can make partially-applying functions to pipe through more painful. Creating curried wrappers around these clunky .NET functions is often a good work-around.
来源:https://stackoverflow.com/questions/43352907/f-piping-to-from-net-objects