let Bindings (F#)
A binding associates an identifier with a value or function. You use the let keyword to bind a name to a value or function.
// Binding a value: let identifier-or-pattern [: type] = expression body-expression // Binding a function value: let identifier parameter-list [: return-type ] = expression body-expression
The let keyword is used in binding expressions to define values or function values for one or more names. The simplest form of the let expression binds a name to a simple value, as follows.
If you separate the expression from the identifier by using a new line, you must indent each line of the expression, as in the following code.
Instead of just a name, a pattern that contains names can be specified, for example, a tuple, as shown in the following code.
The body-expression is the expression in which the names are used. The body expression appears on its own line, indented to line up exactly with the first character in the let keyword:
A let binding can appear at the module level, in the definition of a class type, or in local scopes, such as in a function definition. A let binding at the top level in a module or in a class type does not need to have a body expression, but at other scope levels, the body expression is required. The bound names are usable after the point of definition, but not at any point before the let binding appears, as is illustrated in the following code.
Function bindings follow the rules for value bindings, except that function bindings include the function name and the parameters, as shown in the following code.
In general, parameters are patterns, such as a tuple pattern:
A let binding expression evaluates to the value of the last expression. Therefore, in the following code example, the value of result is computed from 100 * function3 (1, 2), which evaluates to 300.
You can specify types for parameters by including a colon (:) followed by a type name, all enclosed in parentheses. You can also specify the type of the return value by appending the colon and type after the last parameter. The full type annotations for function1, with integers as the parameter types, would be as follows.
When there are no explicit type parameters, type inference is used to determine the types of parameters of functions. This can include automatically generalizing the type of a parameter to be generic.
A let binding can appear in a class type but not in a structure or record type. To use a let binding in a class type, the class must have a primary constructor. Constructor parameters must appear after the type name in the class definition. A let binding in a class type defines private fields and members for that class type and, together with do bindings in the type, forms the code for the primary constructor for the type. The following code examples show a class MyClass with private fields field1 and field2.
The scopes of field1 and field2 are limited to the type in which they are declared. For more information, see let Bindings in Classes (F#).
A let binding at the module level, in a type, or in a computation expression can have explicit type parameters. A let binding in an expression, such as within a function definition, cannot have type parameters. For more information, see Generics (F#).