aboutsummaryrefslogtreecommitdiff
path: root/examples/cppx_blue/Declarations.blue
blob: fe23d62cec0155b1271cee15f23d7a724a1589b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Every declaration of a variable, function, or type is of the following form:
//     name : kind = definition
// where the components mean (and most are optional):
//     name is the name being declared
//     kind is the kind of entity being declared
//     definition can be any expression, including a block expression

// For example, these are declaration statements (and definitions):

widget: type = int;                // define a type

x: widget = 42;                    // a named variable

shape: type = { x: int; y: int; }  // a named type

add: (x:_, y:_) = { return x+y; }  // a named generic function

// add: (x, y) = x+y;              // same, using defaults

// In expression scope, omit name to declare an unnamed entity. For example, these are declaration expressions:
:widget = 42;                      // an unnamed (temporary) object expression

:type = { x: int; y: int; }        // an unnamed type expression

//:(x, y) = x+y                    // an unnamed (lambda) function expression

// Note	These : expressions have very low precedence, so a trailing ; is not needed when lambdas are passed as arguments.
// A local variable declaration statement may omit kind or definition.
// If kind is omitted, it is deduced as if specified _.
// If = definition is omitted, name must be defined by initialization before use (see §3.4.1). For example:

y : int = 42;                       // y is an int with initial value 42

z :     = 42;                       // z, the same, “_” is implicit default

w := 42;                            // same, stylistic convenience

// s: string;                       // declares s, unconstructed

// t = f();                         // constructs t, definite first use