Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Last active April 24, 2021 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseoliv/b055193e790dc41715404956f8c3f039 to your computer and use it in GitHub Desktop.
Save joseoliv/b055193e790dc41715404956f8c3f039 to your computer and use it in GitHub Desktop.
restrict the values a variable can hold
package metaobjectTest
@doc{*
Annotation 'type' should be attached to a type. It checks whether
the value of the type obeys the expression that is attached to
the annotation. Use 'self' as the value of the object that is
being restricted. Therefore, the expressions can be something like:
self >= 0
self >= 0 && self < 5
self[0] == 'a'
*}
object RestrictTo
func run {
var Int@restrictTo{* self >= 0 *} age;
age = 0;
age = 10;
var ok = false;
{
age = -1;
} catch: { (: CyException e :)
ok = true
};
assert ok;
var String@restrictTo{* self[0] == 'a' *} s;
s = "abc";
ok = false;
{
s = "bca";
"error !" println;
} catch: { (: CyException e :)
ok = true
};
assert ok;
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment