Hidden Features of F#

2019-01-09 23:07发布

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

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

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.

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.

11条回答
够拽才男人
2楼-- · 2019-01-09 23:47

See this question

F# operator "?"

for info on the question-mark operator and how it provides the basic language mechanism to build a feature akin to 'dynamic' in C#.

查看更多
男人必须洒脱
3楼-- · 2019-01-09 23:48

I wonder what happens if you add

<appSettings>
  <add key="fsharp-navigationbar-enabled" value="true" />
</appSettings>

to your devenv.exe.config file? (Use at your own risk.)

查看更多
我只想做你的唯一
4楼-- · 2019-01-09 23:48

Passing --warnon:1182 to the compiler turns on warnings about unused variables; variable names that begin with underscore are immune.

查看更多
我命由我不由天
5楼-- · 2019-01-09 23:48

There are no hidden features, because F# is in design mode. All what we have is a Technical Preview, which changes every two month.

see http://research.microsoft.com/fsharp/

查看更多
甜甜的少女心
6楼-- · 2019-01-09 23:49

Inlined operators on generic types can have different generic constraints:

type 'a Wrapper = Wrapper of 'a with
  static member inline (+)(Wrapper(a),Wrapper(b)) = Wrapper(a + b)
  static member inline Exp(Wrapper(a)) = Wrapper(exp a)

let objWrapper = Wrapper(obj())
let intWrapper = (Wrapper 1) + (Wrapper 2)
let fltWrapper = exp (Wrapper 1.0)

(* won''t compile *)
let _ = exp (Wrapper 1)
查看更多
淡お忘
7楼-- · 2019-01-09 23:56

Not really hidden, but as a non-ML person this escaped me for quite a while:

Pattern matching can decompose arbitrarily deep into data structures.

Here's a [incredibly arbitrary] nested tuple example; this works on lists or unions or any combinations of nested values:

let listEven =
  "Manipulating strings can be intriguing using F#".Split ' '
  |> List.ofArray
  |> List.map (fun x -> (x.Length % 2 = 0, x.Contains "i"), x)
  |> List.choose 
     ( function (true, true), s -> Some s 
              | _, "F#"         -> Some "language" 
              | _               -> None ) 
查看更多
登录 后发表回答