Previous Page TOC Next Page



7. Constants

Constants in Blue can be literals or named constants. Literals are available for the predefined manifest classes. Named constants can be created from every legal expression.

7.1 Literals

Literals can be written for all predefined types. They have been introduced in the sections describing the individual types. Here, we just provide a brief summary of possible literal values for the predefined types. For details, refer to the section introducing each type (sections 3.3.1 to 3.3.5).

Literals:

Class: Integer Real Boolean String

Examples: 17 2.0 true "hello"
0 0.0 false ""
-5 -0.122 TRUE "ab\n"

7.2 Named Constants

Named constants are defined in a separate constant definition section, which is preceded by the reserved word const. The constant definition section can precede every variable section. Scope rules are the same as those for variables, and the syntax of the declaration is the same as for variables, except that the initialisation is not optional.

BNF:

const-decls   ::=   const-decl { const-decl }
const-decl   ::=   ident-list  ":" type-def  initialisation 
initialisation   ::=   ":=" expression

Examples:

const

size: Integer := 99
default_title : String := "untitled" printList (l: List) is
== print all list elements const
empty : Boolean := l.isEmpty (false) var
...
do
...
end printList

Note that literals as well as arbitrary function calls may be used for the initialisation of constants. Instance variables cannot be used for the initialisation of instance constants - they are still uninitialised. Care must be taken with function calls: If the function used for initialisation uses instance variables, a runtime error will be generated for using an uninitialised variable. The initialisation of instance constants and variables takes place on object creation before the creation routine is executed.

The exact order of object initialisation is:

• Initialisation of instance constants

• Initialisation of instance variables

• Execution of the creation routine, including (in this order)

• Initialisation of local constants

• Initialisation of local variables

• Execution of routine body

All constants and variables are initialised sequentially in the order in which they are defined. Later definitions may use preceding ones.

7.3 Set Constants

Blue allows sets of literals to be specified. These sets can only appear as literals. No variables of these set types can be created. The only available operator on these sets is in, testing a value for membership in a given set. The in-operation returns a boolean value. (See section 10.4 for more details on the in operation.) Constant sets are also used for case statements (section 11.2).

BNF:

set_expr   ::=   "{" [ set_elem { "," set_elem } ] "}"
set_elem   ::=   expression | subrange
subrange   ::=   expression ".." expression

Examples:

if choice in {1, 2, 3 } then ...

if result in {0..9} then ...

whitespace := ch in {" ", "\n", "\t"}

if ch in {"a".."z", "A".."Z", "_"} then ...

As the examples show, sets can be created by enumeration of values by comma separated lists, subranges, or a combination of both.

Subranges can only be taken from ordinal types. The ordinal types are Integer, String and Enumeration.

This construct does not replace a general purpose set type. It is merely a convenience notation that abbreviates relational comparisons. A general purpose set class can be found in the Blue Standard Collection Library.

Previous Page TOC Next Page