Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
Assignment is to set a value into a variable. This expression is evaluated right to left.
a = b = 10;
You can use a variable without declaration, but the scope depends on the situation. If there is a declaration in outer scope, compiler will refer it. Otherwise, compiler will use it as declared there.
You can use the style of an array or object in assignment. The following three styles are available.
Here is an example.
[a, b, , ...c] = [1, 2, 3, 4, 5, 6]; // 3rd parameter is skipped.
{ x, y } = { x: 20, y: { a: 30, b: 300 } };
{ 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 assignment expresison, 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.
[a, b, , ...c] = [1, 2, 3, 4, 5, 6];
{ x, y } = { x: 20, y: { a: 30, b: 300 } };
{ 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.
{ 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.
[1..10, b] = [1, 100]; // b => 100
[1..10, b] = [2, 100]; // b => 100
[1..10, b] = [9, 100]; // b => 100
[1..10, b] = [11, 100]; // NoMatchingPatternException will occur.
var a = 10;
var b;
{
var a;
a = b = 100;
System.println(a); // => 100
System.println(b); // => 100
}
System.println(a); // => 10
System.println(b); // => 100
100
100
10
100
var a = 10;
{
a = b = 100;
System.println(a); // => 100
System.println(b); // => 100
}
System.println(a); // => 10
System.println(b); // => ???
Error: Symbol(b) is not found near the <test.kx>:8
[a, b, , ...c] = [1, 2, 3, 4, 5, 6]; // 3rd parameter is skipped.
{ x, y } = { x: 20, y: { a: 30, b: 300 } };
{ 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}
[a, b, , ...c] = [1, 2, 3, 4, 5, 6];
{ x, y } = { x: 20, y: { a: 30, b: 300 } };
{ 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.
{ 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)
[1..10, b] = [1, 100]; // b => 100
[1..10, c] = [2, 100]; // c => 100
[1..10, d] = [9, 100]; // d => 100
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
[1..10, b] = [11, 100]; // NoMatchingPatternException will occur.
b = 100
c = 100
d = 100
Uncaught exception: No one catch the exception.
NoMatchingPatternException: Pattern not matched
Stack Trace Information:
at <main-block>(test.kx:8)