DEV Community

loading...

Kinx v0.21.0 Preview Released

krayg profile image Kray-G ・3 min read

Hello everybody!

I published Kinx v0.21.0 preview release! Please see Release Page of Kinx

Introduction

At the version 0.21.0 it newly supported an assignment by a pattern matching, case-when, and switch-when syntax.

Pattern Matching

The pattern matching is very useful. For example, you can use it in a declaration like this.

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 } };
Enter fullscreen mode Exit fullscreen mode

At the last case, the right hand side is not matched by the pattern of the left hand side. Therefore you will see an exception of NoMatchingPatternException.

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)
Enter fullscreen mode Exit fullscreen mode

You can use a same feature also at an assignment, an argument of a function, and case-when.

Please look at the description for the related features as follows.

Switch-When

I thought I would like to make a case-when being not fallthrough be substitute of a switch-case being fallthrough.
But there was a problem for the performance on it.

  • case-when has been designed that the condition is evaluated by a pattern matching. This means the order of evaluating a condition is sequential from the top.
  • On the other hand, switch-case is designed the order of evaluating a condition is not defined. This means, it will be translated to just a if-else, or it will be translated to a binary search, or it will translated to a jump by table. In particular, a jump by table is important at the point of a performance.

Therefore I have introduced switch-when instead of switch-case. You can use a when label instead of a case label. If you use a when label, the break statement will be automatically inserted after the last statement of when clause.

Of course you can use an else: label instead of a default: label. An else: label means a default: with break.

If you want to use a fallthrough in when, you can also use a fallthrough keyword at the last statement in when.

Conclusion

I sometimes received a reaction like ''Why did you select a fallthrough in switch-case?'', and ''It is a bad selection.'' I believe switch-when will be an answer for that. One of weakpoints in Kinx will be eliminated.

I hope you will be a user of Kinx and try something with this language. I will welcome any feedback.

See you!

Discussion (0)

pic
Editor guide