F# piping to/from .net objects

橙三吉。 提交于 2019-12-11 04:46:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!