往返于.net对象的F#管道 [英] F# piping to/from .net objects
问题描述
我注意到在一些.NET方法之间进行管道传递有困难.玩具示例
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
我基本上是想弄清楚何时可以将管道与.NET对象结合使用,何时不可以.如果有参考,我很想获得链接!
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!
推荐答案
对于您在使用字符串时遇到的挂断,下面的链接表明F#4.0中已添加了将构造函数视为函数的支持
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
另一个使.NET库中的管道笨拙的常见情况是,它们作为tupled
(而不是curried
)函数参数公开,这会使部分应用的函数更加麻烦.围绕这些笨拙的.NET函数创建咖喱包装通常是一个不错的解决方法.
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.
这篇关于往返于.net对象的F#管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!