Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
Declaration statement is the statement to declare variables.
var a; // initializing 'a' to null
Declaration of a variable has initializer to initialize it.
If there is no initializer, the variable should be null
automatically.
var a = 10; // initializing 'a' to 10 as integer.
Multiple variables can be put in one declaration statement separated by ,
.
Each variable can have its own initializer, or can have no initializer.
var a, b;
var x = 10, y;
Variable can be constant by declaration with const
.
const a = 10; // initializing 'a' to 10 as integer and it is constant value.
Constant value can not be modified and it will be compile error when trying to change that variable’s value.
const a = 10;
a = 20; // error.
Destructuring assignment is supported and a spread/rest operator is also available. But a spread/rest operator of destructuring assignment for Object is not supported.
var [a, ...b] = [10, 20, 30];
// => a == 10
// => b == [20, 30]
Of course, assignment is rejected when it declared as const
.
const [a, ...b] = [10, 20, 30];
a = 10; // error.
You can use the style of an array or object in assignment. The following three styles are available.
Here is an example.
var [a, b, , ...c] = [1, 2, 3, 4, 5, 6]; // 3rd parameter is skipped.
var { x, y } = { x: 20, y: { a: 30, b: 300 } };
var { x: d, y: { a: e, b: f } } = { x: 20, y: { a: 30, b: 300 } };
System.println("a = ", a);
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
System.println("e = ", e);
System.println("f = ", f);
System.println("x = ", x);
System.println("y = ", y);
Here is the result.
a = 1
b = 2
c = [4, 5, 6]
d = 20
e = 30
f = 300
x = 20
y = {"a":30,"b":300}
When it is an declaration, the pattern matching is available.
If a part of variables is a literal, it will be checked if the same value.
And if matching a pattern is failed, the exdeption of NoMatchingPatternException
will be raised.
Here is an example.
var [a, b, , ...c] = [1, 2, 3, 4, 5, 6];
var { x, y } = { x: 20, y: { a: 30, b: 300 } };
var { x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 300 } };
System.println("a = ", a);
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
System.println("e = ", e);
System.println("x = ", x);
System.println("y = ", y);
// => .y.b requires 300, but it is 3 in actual.
var { x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 3 } };
Here is the result.
a = 1
b = 2
c = [4, 5, 6]
d = 20
e = 30
x = 20
y = {"a":30,"b":300}
Uncaught exception: No one catch the exception.
NoMatchingPatternException: Pattern not matched
Stack Trace Information:
at <main-block>(test.kx:14)
If you write a Range in a pattern, it will check inside that Range.
Here is an example.
var [1..10, b] = [1, 100]; // b => 100
var [1..10, c] = [2, 100]; // c => 100
var [1..10, d] = [9, 100]; // d => 100
var [1..10, e] = [11, 100]; // NoMatchingPatternException will occur.
var a;
System.println(a.isUndefined ? "null" : "defined");
null
var a = 5;
System.println(a);
5
function pow8(n) {
return n ** 8;
}
var a = "2 ** 8 = " + pow8(2);
System.println(a);
2 ** 8 = 256
var a, b = 10, c, d = "abc";
System.println(a.isUndefined ? "null" : a);
System.println(b.isUndefined ? "null" : b);
System.println(c.isUndefined ? "null" : c);
System.println(d.isUndefined ? "null" : d);
null
10
null
abc
const a = 10;
a = 20;
Error: Can not assign a value to the 'const' variable near the <test.kx>:2
If you did not initialize a constant variable, you can initialize it only one time by assignment.
const a;
a = 20;
System.println(a);
20
It should be error when initializing twice.
const a;
a = 20;
a = 30; // error.
Error: Can not assign a value to the 'const' variable near the <test.kx>:3
Even if it is Object, it should be compile error.
const a = { x: 10 };
a = { y: 20 };
Error: Can not assign a value to the 'const' variable near the <test.kx>:2
But the object member can be modified.
const a = { x: 10 };
a.x = 20;
System.println(a.x);
20
var [a, ...b] = [10, 20, 30];
System.println(a);
System.println(b[1]);
10
30
const [a, ...b] = [10, 20, 30];
a = 10;
b = 20;
Error: Can not assign a value to the 'const' variable near the <test.kx>:2
Error: Can not assign a value to the 'const' variable near the <test.kx>:3
var [a, b, , ...c] = [1, 2, 3, 4, 5, 6];
var { x, y } = { x: 20, y: { a: 30, b: 300 } };
var { x: d, y: { a: e, b: f } } = { x: 20, y: { a: 30, b: 300 } };
System.println("a = ", a);
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
System.println("e = ", e);
System.println("f = ", f);
System.println("x = ", x);
System.println("y = ", y);
a = 1
b = 2
c = [4, 5, 6]
d = 20
e = 30
f = 300
x = 20
y = {"a":30,"b":300}
var [a, b, , ...c] = [1, 2, 3, 4, 5, 6];
var { x, y } = { x: 20, y: { a: 30, b: 300 } };
var { x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 300 } };
System.println("a = ", a);
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
System.println("e = ", e);
System.println("x = ", x);
System.println("y = ", y);
// => .y.b requires 300, but it is 3 in actual.
var { x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 3 } };
a = 1
b = 2
c = [4, 5, 6]
d = 20
e = 30
x = 20
y = {"a":30,"b":300}
Uncaught exception: No one catch the exception.
NoMatchingPatternException: Pattern not matched
Stack Trace Information:
at <main-block>(test.kx:14)
If you write a Range in a pattern, it will check inside that Range.
Here is an example.
var [1..10, b] = [1, 100]; // b => 100
var [1..10, c] = [2, 100]; // c => 100
var [1..10, d] = [9, 100]; // d => 100
System.println(b);
System.println(c);
System.println(d);
var [1..10, e] = [11, 100]; // NoMatchingPatternException will occur.
100
100
100
Uncaught exception: No one catch the exception.
NoMatchingPatternException: Pattern not matched
Stack Trace Information:
at <main-block>(test.kx:8)