F # and the tuple

At http://diditwith.net , I see that in F # it is not necessary to pass parameters to a function that otherwise requires them. The language automatically activates the result and the output parameter in the tuple. (!)

Is it some kind of side effect (pardon of puns) of general language mechanics or a feature that was specifically formulated in the F # specification and intentionally programmed into the language?

This is an awesome feature, and if it was explicitly placed in F #, I wonder what other similar piles of such gold are hidden in the language, because I looked at dozens of web pages and read three books (D. Sime, T. Petrichek and K. Smith), and I did not see this particular trick at all.

EDIT: As Mr. Petrichek replied below, he mentions this function in at least two places in his book Functional Programming in the Real World. My bad.

+4
source share
2 answers

This is not a side effect of any other, more general mechanism in F #.

It has been specifically added for this purpose. .NET libraries often return multiple values ​​by adding out (or ref ) parameters at the end of the method signature. In F #, returning multiple values ​​is done by returning a tuple, so it makes sense to turn the .NET style into a typical F # pattern.

I don't think F # does a lot of these tricks, especially when it comes to interoperability, but you can view some handy snippets here and.

(I quickly checked, and real-time functional programming outlines the trick on pages 88 and 111.)

+5
source

This is a special function that makes interaction with .NET methods more enjoyable - all final parameters can instead be considered as part of the return value (but note that this only affects completion parameters, therefore a method with a C # signature, for example, void f(out int i, int j) cannot be called this way).

Perhaps the out parameters are just a way to get around the lack of tuples in .NET 1.0. It seems that many methods that use them will be written differently if they target later versions of the framework (using Nullable<_> types or tuples as return types).

+3
source

All Articles