Variable Declaration

Summary

Declares a variable. The variable can optionally be initialized to a value.

Syntax

type name1[= value1 [, name2 [, name3 ... [, nameN]]]];

Parameters

type
The type for the variable(s).
nameN
The name for the variable. This can be any valid identifier.
valueN
Optional. Initializes the variable to a value. This can be any valid expression.

Description

Variable declarations in JS++ are typed. In other words, a type must be specified along with the variable declaration.

The scope of the variable is the nearest block; if no block is present, the variable will be scoped to the file's top-level scope.

Constants

Constants can be declared using the final keyword as a modifier for the variable declaration.

1
2
final int x = 10;
x = 11; // ERROR!

When final is applied to a variable declaration, the variable must be initialized. The initialized value or reference is the "final" value/reference for the variable. It cannot be modified further, and properties cannot be added to or deleted from the variable. For primitive types, the final keyword will create a constant. For reference types, the object that is being referenced by the variable cannot be changed.

Although a const keyword is reserved by the language, it is not used. This is by design. If a variable is initialized to an object reference, the object can still be modified (e.g. via other references to the same object). Thus, this creates a false sense of security and can be a source for confusion; in order to encourage the highest code quality, these factors had to be minimized. Therefore, final was used instead of const. "Constants" can still be created by using the final modifier on variable declarations with a primitive type.

Callback Types

JS++ allows typed callbacks.

1
2
3
int area(int length, int width) = int(int length, int width) {
    return length * width;
};

The parameters in the type annotation must be named. The reason for this was a design choice; in order for the language to be intuitive, it had to be consistent; since function parameters with callback types required the callback's parameters to be named (for the purposes of documentation), this was carried over to variable declarations as well. The names of the parameters from the type annotation cannot be used. They are merely for readability and documentation.

External Type Variable

If the variable deals with a JavaScript type, use the var keyword.

Differences to JavaScript

JS++ fixes a lot of the bugs with variables in JavaScript's language design including:

Scoping

In JavaScript, the var keyword declares a function-scoped variable. In JS++, variable declarations create block-scoped variables. JS++ was designed this way because, typically, most JavaScript variables end up being declared as though they were block-scoped anyway so this was only seen as a minor change in the semantics.

Hoisting

Additionally, JavaScript will "hoist" on the var keyword. Variable declarations in JavaScript are processed before they are executed, and they are scoped to the nearest function. In other words, a variable can be used before its declaration as long as the usage is in the same scope as the declaration.

The following code is logically incorrect but valid JavaScript:

1
2
var y = x + 1; // NaN
var x = 2;     // 2

If we run the above JavaScript code through the JS++ compiler, we will receive a compile-time error instead:

[ ERROR ] JSPPE0001: 'x' has not been declared at line 1 char 8
  FAILED  Compiled in 71ms: (1) errors and (0) warnings

This is because JS++ does not "hoist" variable declarations. In JS++, a variable cannot be used until it has been declared. Like with the scoping design, well-written JavaScript does not "hoist" variables anyway so the languages remain semantically similar.

Implicit Globals

In JavaScript, if you assign a value to a variable that was not declared (including typos), it will implicitly declare the variable as a global variable and assign the value to it. First of all, there is no global scope in JS++. Secondly, implicit global variable declarations are not possible in JS++ because all variables must be declared before they can be used; otherwise, the compiler will raise an error at compile time.

ReferenceErrors

If you attempt to access a variable without declaring it in JavaScript, you will receive a ReferenceError (including for typos). While implicit global variables will be declared if you assign (write) a value to an undeclared variable, a ReferenceError will be thrown at runtime if you try to access (read) a variable that has not been declared in JavaScript.

ReferenceErrors must be manually caught in JavaScript; otherwise, the JavaScript program will crash at runtime:

1
2
3
var counter = 10;
var copy = counteer; // Typo. ReferenceError crashes the program.
foobar(); // This never runs because the program has already crashed.

However, a ReferenceError is not possible in JS++. All variables must be declared before use (whether reading/accessing or writing/assigning). Variables that are used before declaration or without declaration will automatically result in a compile-time error.

Examples

Declaring a variable
1
int foo;
Declaring multiple variables of the same type in one statement
1
int foo, bar, baz;
Declaring and initializing a variable
1
int foo = 1;
Declaring and initializing multiple variables of the same type in one statement
1
int foo = 1, bar = 2, baz = 3;

See Also

Share

HTML | BBCode | Direct Link