M provides two primary means for values to come into existence: computed values and stored values (aka fields). Computed and stored values may occur with both module and entity declarations and are scoped by their container.
A computed value is derived from evaluating an expression. In contrast, a field stores a value and the contents of the field may change over time. [1]
A ComputedValueDeclaration binds a name to an expression that is used to compute the resultant value.
ComputedValueDeclaration: Identifier FormalParameters ReturnTypeopt ExpressionBody FormalParameters: ( Parametersopt ) Parameters: Parameter Parameters , Parameter Parameter: Identifier TypeAscriptionopt ReturnType: TypeAscription ExpressionBody: { Expression ;opt }
If the type of a parameter or the ReturnType is not explicitly specified, the parameter or return type will be implicitly inferred from the ExpressionBody. For example, the following ComputedValueDeclarations have the same meaning:
Add(x : Integer32, y : Integer32) { x + y } Add(x : Integer32, y : Integer32) : Integer32 { x + y }
ComputedValueDeclaration introduces the formal parameters into scope. It is an error to have more than one formal parameter with the same Identifier.
A field is a storage location. A field declaration specifies the name of the field and optionally a type ascription that constrains values in the field to be of the ascribed type. An initial value may be defined by either equating the field with an Expression
or using an InitializationExpression (Section 5.3).
If an initializer is present without a TypeAscription, the type of the field is the type of the initializer. If both an initializer and TypeAscription are present, then the initializer must conform to the TypeAscription.
FieldDeclaration: DottedIdentifer TypeAscriptionopt = Expression ; DottedIdentifier TypeAscriptionopt InitializationExpression ;opt DottedIdentifer TypeAscriptionopt ; DottedIdentifer IdentifierPath . IdentifierPath IdentifierPath Identifier IdentifierPath . Identifier
[1] M currently provides no language constructs for mutating the contents of a field. However, implementations are likely to provide out-of-band mechanisms for update.