I have a variable x = value: |java+interface:///java/util/Iterator|. How to convert this to x = loc: |java+interface:///java/util/Iterator|?
Simple answer, pattern matching is like casting. You can use it in different ways to make a static type more concrete:
value x = ... ;
if (loc l := x) {
// here l will have the type `loc` and have the same value that `x` had
}
or:
value x = ...;
bool myFunc(loc l) = ... ;
myFunc(x); // /* will only be executed if x is indeeed of type `loc`
or, you can use pattern matching to filter a list:
list[value] l = ...;
list[loc] ll = [ e | loc e <- l ];
or, a switch:
switch(x) {
case loc l: ...;
}
etc. :-)
A generic cast function can also be written, but I think it's a code smell to use it and it makes code harder to understand:
&T cast(type[&T] t, value x) {
if (&T e := x)
return e;
throw "cast exception <x> can not be matched to <t>";
}
value x = ...;
loc l = cast(#loc, x);