Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use parentheses when calling a function in f#?

Tags:

f#

I'm learning about f# and I understand you don't need to use parentheses when calling a function.

Ex

let addOne arg1 =
    arg1 + 1

addOne 1

vs

this.GetType()

Why do I have to use parentheses on the second function?

like image 719
user3587180 Avatar asked Jun 01 '17 22:06

user3587180


People also ask

What is parentheses in function call?

When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.

Do functions need parentheses?

Without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. We don't use the parentheses in that code because we don't want the function to be called at the point where that code is encountered.

What is the difference between calling function with parentheses and without in JavaScript?

With parenthesis the method is invoked because of the parenthesis, the result of that invocation will be stored in before_add. Without the parenthesis you store a reference (or "pointer" if you will) to the function in the variable.

What are the parentheses functions?

Parentheses are used to enclose incidental or extra information, such as a passing comment, a minor example or addition, or a brief explanation. The writer may choose to put additional information within parentheses or to set off the text using dashes or commas.


1 Answers

There is a bit of a mismatch between working with .NET libraries and working with F# libraries when it comes to parameters, but you can generally see () not as parentheses, but as a special value of type unit that means "no useful information".

This means that when you say:

addOne 1

You are calling addOne with a value - number 1 - as a parameter. Now, when you apply the same reading to the second example:

this.GetType()

You can read this as calling this.GetType with a value - the special () unit value as a parameter. If you wanted to be consistent, you could write this with space too:

this.GetType ()

In practice, most people will omit the space when calling .NET libraries. When you do not write the space, F# also supports method chaining so you can write e.g. foo().bar().

Many F# functions taking multiple parameters will use the "curried" form, which means that the parameters need to be separated by spaces. For example:

let add a b = a + b
let mul a b = a * b

add 10 (mul 20 3)

Here, you need parentheses around the second expression, so that the compiler knows how to parse the code. This is in contrast with typical .NET methods, which take parameters as a tuple. F# tuples are written as (10, "hello") and so you can see a method call as an ordinary call accepting a tuple:

some.Operation (10, "Hello")

Again, typically you wouldn't write the space here, because you know this is actually a .NET method call, rather than "passing tuple to a function", but conceptually, you can think of it in both ways.

This is the summary - there are a few corner cases where method calls do not really behave like tuples (e.g. when it comes to named parameters), but this way of thinking about it should give you an idea about how things work.

like image 93
Tomas Petricek Avatar answered Sep 18 '22 14:09

Tomas Petricek