F#的隐藏功能 [英] Hidden Features of F#

查看:107
本文介绍了F#的隐藏功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是类似的 C#问题的不懈尝试.

This is the unabashed attempt of a similar C# question.

那么您最喜欢的F#隐藏(或不隐藏)功能是什么?

So what are your favorite F# hidden (or not) features?

到目前为止,我所使用的大多数功能并未完全隐藏,但相当新颖.与说C#或VB.NET相比,重载运算符是多么微不足道的事情.

Most of the features I've used so far aren't exactly hidden but have been quite refreshing. Like how trivial it is to overload operators compared to say C# or VB.NET.

Async<T>帮助我省掉了一些真正的丑陋代码.

And Async<T> has helped me shave off some real ugly code.

我还是该语言的新手,所以很高兴了解野外正在使用的其他功能.

I'm quite new to the language still so it'd be great to learn what other features are being used in the wild.

推荐答案

可以通过提供名称以NumericLiteral开头并定义某些方法(FromZeroFromOne等)的模块来定义用户定义的数字文字. ).

User defined numeric literals can be defined by providing a module whose name starts with NumericLiteral and which defines certain methods (FromZero, FromOne, etc.).

尤其是,您可以使用它来提供更易读的语法来调用LanguagePrimitives.GenericZeroLanguagePrimitives.GenericOne:

In particular, you can use this to provide a much more readable syntax for calling LanguagePrimitives.GenericZero and LanguagePrimitives.GenericOne:

module NumericLiteralG = begin
  let inline FromZero() = LanguagePrimitives.GenericZero
  let inline FromOne() = LanguagePrimitives.GenericOne
end

let inline genericFactorial n =
  let rec fact n = if (n = 0G) then 1G else n * (fact (n - 1G))
  fact n

let flt = genericFactorial 30.
let bigI = genericFactorial 30I

这篇关于F#的隐藏功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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