设计可从F#很好使用的C#库的准则 [英] Guidelines to design a C# library well usable from F#

查看:69
本文介绍了设计可从F#很好使用的C#库的准则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想指出,这不是问题的反面

最佳F#和C#一起使用的设计F#库的方法

在这里,我不问如何设计一个用C#编写的功能库,以便在两个领域中都可以使用.

我想了解良好做法,了解哪些设计选择可以接受或避免,以便做出合理的折中以使该库可从F#使用 >.

实践(例如):

  • 保持对象层次尽可能简单

  • 避免对象的变异状态,但返回新的对象

  • 等...

任何已经做过的人都可以分享它的经验吗?

旁注

有趣的是,这个OSS项目 IronJS .是的,它是用F#编写的,但是作者公开了两个专门的主机IronJS.Hosting.FSharpIronJS.Hosting.CSharp.

解决方案

想象一下,有一天您想用F#重写C#库以提高可用性.这是您可能采用的路径:

我专注于路径命令式C#->功能性C#->功能性F#->惯用的F#". C#库的功能越多,库在F#中的可用性就越高.功能样式有助于提高可组合性,并且更接近惯用的F#代码.按照这些思路,您可以:

  • 默认采用不变性原则.如果您不知道以后是否需要更新字段/属性,请先将其标记为readonly.
  • 遵循基于表达式的声明式编程风格. LINQ操作就是很好的例子.
  • 以不可变的方式使用不可变的集合或可变集合.使用 F#的乐趣和利润活动模式分解对象层次结构.

    I just want to point out that this is question is not the reverse of

    Best approach for designing F# libraries for use from both F# and C#

    Here I'm not asking how to design a functional library written C# to be used in both worlds.

    I'd like to know good practices on what design choices embrace or avoid to get a reasonable compromise for make this library usable from F#.

    Practices like (for example):

    • keep object hierarchy as simple as possible

    • avoid mutating state of objects, but return new ones

    • etc...

    Anyone that already done it, can share it's experience?

    Side note

    It's interesting note this OSS project, IronJS. Yes, it's written in F#, but the author expose two specialized host IronJS.Hosting.FSharp and IronJS.Hosting.CSharp.

    解决方案

    Imagine one day you would like to rewrite your C# library in F# for better usability. Here are the paths you are likely to take:

    I focus on the path "Imperative C# --> Functional C# --> Functional F# --> Idiomatic F#". The more functional your C# library is, the more usable your library is in F#. Functional style helps increase composability and is closer to idiomatic F# code. Along these lines, you can:

    • Embrace immutability by default principle. If you don't know whether you need to update a field/property later, just mark it readonly first.
    • Follow expression-based and declarative programming style. LINQ operations are good examples.
    • Use immutable collections or mutable collections in an immutable way. With the introduction of C# immutable collections, it should be easier than ever before.

    The picture above is taken from F# for fun and profit's Porting from C# to F# series. They are very helpful; knowing how C# concepts are expressed in F# will improve usability of your library.

    It's hard to avoid C#'s object-oriented features. Remember that F# type inference doesn't work very well with these features. Along the line of keeping object hierarchy simple, you should reduce number of member overloads. A big number of member overloads will easily confuse F# type checker. Moreover, it doesn't hurt to distribute a thin F# wrapper with your C# library. Certain things you need to do are turning some methods into module functions and creatingActive Patterns to decompose object hierarchy.

    这篇关于设计可从F#很好使用的C#库的准则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆