Alex
Alex

Reputation: 2382

F# constructor syntax

when using .net classes, there are cases when we don't need parenthesis to pass a single parameter like

let foo = DirectoryInfo "boo"

but something a little more complicated using a single parameter, we do need parenthesis... does anyone know the parsing rules when this is true?

Upvotes: 5

Views: 434

Answers (1)

wsanville
wsanville

Reputation: 37516

In F#, all functions take a single parameter. Now this may be a little confusing at first, because you can have a function which appears to take more than one parameter, but you're actually passing a single parameter that is a tuple.

Here's a simple example of constructors that appear like they take more than 1 parameter:

let foo = DirectoryInfo "boo" //passing 1 argument
let foo2 = DirectoryInfo ("boo") //passing 1 argument that is a tuple
let foo3 = StringBuilder ("blah", 100) //passing 1 argument again
let foo4 = StringBuilder "blah" 100 //does not compile

For more info about this style, check out Tuples on MSDN.

Now, there also is another slightly different method of making it seem like a function takes more than 1 argument. This is called currying, which you will see more often when dealing only with F# code. Here's a quick example of this.

Upvotes: 4

Related Questions