Alexander Ryan Baggett
Alexander Ryan Baggett

Reputation: 2397

How to Alias .NET functions in F#

So in F# functions are supposedly first class citizens since it is a functional programming language and can be referenced.

So I can do this just fine

    let double x = x * 2

    let times2 = double

And now times2 is an alias for double, and has the same function signature int -> int

Why can't I do this to alias the .NET function?

   let write = System.IO.File.WriteAllLines

If I try to do this it thinks I am missing a parameter, but in reality I am not trying to invoke the function, I am trying to alias it.

The member or object constructor 'WriteAllLines' does not take 1 argument(s). An overload was found taking 2 arguments.

Is there a way to alias predefined .NET functions or is it not possible?

Upvotes: 3

Views: 264

Answers (1)

Gus
Gus

Reputation: 26204

Well in fact System.IO.File.WriteAllLines is not an F# syntactic function. It's a static member, so it can be (and indeed it is) overloaded. Then the compiler doesn't know which overload to pick up, he just need an additional hint from you.

You can either supply arguments:

let write x y = System.IO.File.WriteAllLines(x, y)

or some type annotations:

let write  = System.IO.File.WriteAllLines : _ * _ -> _

which resolve to a unique overload.

Upvotes: 8

Related Questions