Skip to content

Instantly share code, notes, and snippets.

@joseoliv joseoliv/Range.cyan
Created Jun 15, 2018

Embed
What would you like to do?
Metaobject range
package metaobjectTest
@doc{*
Ranges are attached to types. A variable of type Int@range(first, last)
can only contain values between first and last (inclusive). This is
checked at compile-time when a literal is assigned to the variable
and at run-time if an expression is assigned to the variable.
*}
object Range
func run {
var Int@range(1, 12) hour;
hour = 7; // ok
// hour = 20; // compile-time error
let outOfLimitsHour = 30;
{
// runtime error
hour = outOfLimitsHour;
} catch: { (: ExceptionStr e :)
// do nothing
};
var Int@range(1, 24) hour24;
hour24 = hour; // ok
// hour = hour24; // compile-time error
// other examples
var Char@range('a', 'c') ch_a_c;
var Char ch;
ch = 'a';
ch_a_c = 'a';
ok = true;
{
ch_a_c = ch;
} catch: { (: ExceptionStr e :) ok = false };
assert ok;
ch = 'e';
ok = false;
{
ch_a_c = ch;
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
var Int@range(1, 12) month;
var Int@range(5, 10) n57;
n57 = 6;
month = n57;
p = 13;
month = 4;
ok = false;
{
var v66 = [ month, p, 7 ];
} catch: { (: ExceptionStr e :) ok = true };
assert !ok;
ok = false;
{
var m787 = [ month -> month, p -> 5, 4 -> 1 ];
} catch: { (: ExceptionStr e :) ok = true };
assert !ok;
ok = false;
{
var m676 = [ month -> month, 5 -> p, 5 -> 9 ]
} catch: { (: ExceptionStr e :) ok = true };
assert !ok;
ok = false;
{
var k67 = month;
k67 = p;
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
ok = false;
{
let Int@range(0, 1) k69 = p;
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
ok = false;
{
var Int@range(0, 1) n5656 = 0;
n5656 = n5656 + 1;
n5656 = n5656 + 1
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
ok = false;
{
var Int@range(0, 1) n5656 = 0;
n5656 = n5656 - 1;
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.