Edit: nevermind, I might promote it when it's more generally available.
HN user
etalian
A language that I am working on has those things "built-in" (it still generates compilable C++ in the backend). I did recently add pattern matching (and not just on variants) where the above would be equivalent to:
$v:|[:int, :bool, :double] = 5;
$vi: = v.[:int];
$holds_int: = v.?[:int];
switch v {
.[:int]$i { /std cout << "got int:" << i };
.[:bool] { /std cout << "got bool" };
.[:double]$d { /std cout << "got double: " << d };
};
If a type repeats in the same variant, you would match by index to tell them apart: $v:|[:int, :int]<(.[0] = 5);
switch v {
.[0]$i0 { /std cout << "got first int:" << i0 };
.[1]$i1 { /std cout << "got second int: " << i1 };
};
(I have some ideas to allow named labels instead of numerical indices but that is not yet implemented)It is, in spirit, an implementation of the inspect proposal [1], but with a (subjectively) much simpler and more powerful syntax (the grammar of the entire language is fully LALR(1) without ambiguities).
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p137...
I wondered the same a while ago, and it turns out you can, in fact to test this hypothesis I ended up implementing such a language myself. The resulting syntax can express every construct from modern C++, is fully LALR(1) (no ambiguities and no vexing parses), has fewer keywords and is in general shorter than the equivalent C++ code, and once you know the syntax it is (subjectively) easier to read too (no spiral rule for example). Plus of course it can fully interoperate with existing C++.
Have been waiting to make it open source until I have finished writing the user manual (aiming for the end of this year), if that's interesting to you I post updates about it on twitter at cigmalang.