In the previous tutorial, "F # and functional Programming Overview" We learned some of the features of F # and functional programming, more about F # language and functional programming can refer to the 51CTO interview with Microsoft MVP Zhao, "TechED 09 video interview: F # and functional programming languages". In this tutorial we will learn some of the basics of F #, before we begin, let's brush up on our Hello World code:
#light
System.Console.WriteLine ("This
is one Hello ")
Printfn "This
is another Hello "
F # is a mixture of functional and object-oriented objects. It sometimes looks strikingly similar to C # or Visual Basic, but it's completely unfamiliar. F # programs consist of a series of expressions, each of which can be specified by a "let" identifier, such as:
Let Fles = System.IO.DirectoryInfo (@ "C:\Users\Chance").
GetFiles ()
In the above code, "Fles" is assigned a value, in this case, a file path. Interestingly, the actual type of "fles" is not defined in detail until the statement gets the return value on the right. You may feel a little awkward, in Java or other programming languages, variable fles should be defined as a data type, string, or whatever type to be explicitly treated differently in memory, but these rules are somewhat different in F #. This also leads to a slight difference in our F # concise tutorials, and we do not introduce F # 's basic data types as usual tutorials, in the sense that F # can be of any type or only one type.
F # Tip: F # is a type inference language that is inferred and determined during compilation. If you write F # in Visual Studio, pointing the mouse at a value will get its type, and the compiler can infer the type from the definition of the function body or other means; Visual Studio is the primary tool for developing F #, 51CTO recommends you to read visual Studio 2010 about F # resources in the article.
Type inference we say that the type of the data is inferred, because F # 's compile-time process attempts to determine its type based on the characteristics of the variable itself and to ensure that the type is safe. Although F # is a strongly typed language, the type declaration of a variable is not necessary in the judgment inference process of the type.
Type inference has its own advantages. When using F # to develop some large applications, such as. NET and Java developers are familiar with generic features (generics) are done by type inference. Note that the F # compiler sees any expression with no type annotation as generic. For example, in the following function, the types of each variable are defined (inferred) as generics, even if the program writer does not define any types.
Let f x =
Let y = g x
H y
Let f (x: ' a): ' B =
Let y: ' C = (g: ' a-> ' C) x
(H: ' c-> ' B) y
F # Tip: In F #, a generic type parameter is a character prefixed by an apostrophe. For example, the ' B and ' C in the example above is the most commonly used generic parameter. Like in. NET, a generic type can also use the angle bracket syntax, such as "dictionary< ' Key, ' value>." When you have only one generic parameter, you sometimes see it using ' prefix ' syntax instead of angle brackets--most commonly used with F # generic class ' list ' and ' option '. For example, "Intlist" and "list<int>" express the same function, but the writing method is different.
F # type inference mechanism
Most type inference in the F # language can follow the following two rules. First, if a function is used to produce a value, the compiler assumes that the type of the value is a function requirement. Second, if a value is an inevitable result of an expression, the type of the value is determined by this expression.
In some cases these simple rules are not complete enough and the compiler must require a type declaration. For example, when an arithmetic operator is used, F # handles it very carefully and does not assign a numeric type to another without the programmer's explicit code. This is done to ensure that the type inference does not add to the burden on the compiler when a large number of numerical computations are performed by F #.
An example of a second rule occurs when the method is overloaded. For example, the Write method has 18 loads in system.console (. NET, system.console encapsulates input, output, and error stream operations based on console applications). Type inference can determine the type that is passed to it, but cannot determine the type of value transferred in another direction.
Type inference is not just a simple symbol, it can also be used for the detection of program functionality. When you write a piece of code, the type inference function intelligently obtains the specified type for the code, which means that the error is not introduced into the program. This mechanism enables F # to gain the simplicity of code in dynamic languages while ensuring a completely static type system.
For more on F # 's type and syntax basics please refer to:
F # Data type: discriminator Union
F # Basic syntax, pattern matching and list
The type system and type inference mechanism of F # is the foundation of learning and understanding F # language and mastering these benefits for our later learning. Next week we will continue our study of F # to explore the basic syntax of F #.