Wednesday, March 3, 2010

Song From The Sims 3 Electro Song

F # and the use of out parameters

Each function or method can only ever return a zunächstmal information. This is expressed by the return value or return parameters. What do you do but if you want to return in a way more information that maybe will even be optional? One possibility is of course a complex data type to define and designate these as the return type of the method. For convenience, but rather not make the man.

a most pragmatic approach is to provide the additional return values each out to define parameters. The out parameters are generally primitive data types such as string or int .

In. NET Framework, there are some methods which have a out parameters. A class is often used as the Dictionary .

Here's an example:

var dict = new
Dictionary \u0026lt;string , int> ();

dict.Add ( "A" , 1);

dict.Add ( "B" , 2);


We are now trying to pull values from the dictionary. Since it can not be guaranteed that the value sought is in the dictionary, we use the method TryGetValue . Since this is now a out parameters defined, we need before we can call this method a variable with the corresponding parameter type out and define the method with the return value of variable aufrufen.Der TryGetValue is a bool so that can be seen, whether the value in the Dictionary is available. Only if the value exists, also has the variable value. But be careful! Since int is a value type, he always has a default value, which is 0 For safety, one should therefore out variables to assign a value other than the default value at int example: -1 instead o.


int value = -1;

var success = dict.TryGetValue ( "A" , value);

if (success) {



Console WriteLine (value). ;

}

extent is likely to be clear and certainly have been made many times. For me, the above method, as TryParse a legitimate reason to use out parameters. All other reasons are of secondary importance since convenience is a priority. They want to halt any complex data types to create only one return parameter.

But there is another way: # F!

F # to the rescue

Because F # is a normal. NET is language, of course you can also call the classes of the framework. For example, the methods out define parameters such as as the Dictionary. How does F # with it?

In F # we can normally produce a dictionary.

open System.Collections.Generic

let dict = new Dictionary \u0026lt;string,int> ()

Now, we add values to the Dictionary

dict.Add ( "A" , 1)

dict.Add ( "B" , 2)

Finally, we want to read the values again.

let result = dict.TryGetValue ( "A" )

Now what type of result is now? And why do you not pass a out parameters?

F # Interactive is the answer.

val result: bool * int = (true, 1)

Result is of type bool * int
what is means in F # that this is a tuple. . NET 4.0 can describe this as Tuple \u0026lt;bool,int>
.

Now we can use this tuple to the functions fst and disassemble snd .

let success = fst result

val success: bool = true


let value = snd result

val value: int = 1

F # F # would not be if it could not even express simple. One can namely the tuple directly into its component parts.

let (success, value) = dict.TryGetValue ( "A" )

val value: int = 1

val success: bool = true

let (success, value) = dict.TryGetValue ( "C" )

val value: int = 0

val success: bool = false

Conclusion

to me that this is a good example of how expressive is F #. In the Vergangeneheit many errors in the programming language design have been made. Above all, C + +, but unfortunately, even with C #. Tuples are a Brilliant means to avoid out parameters. Since tuple. NET 4.0 is a "first class" data type, there is no reason to do so in C # to a hollowing out as an alternative to parameters. Ultimately, it makes more fun but with F #!

Over and out !

0 comments:

Post a Comment