简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM