Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Person {
- /**
- * Accessors are compact. Invariants can use assertions for validation.
- * Unless otherwise qualified, the body defines the setter characteristics only.
- */
- accessor notnull int age {
- assert $ > 0 : "Must be greater than 0";
- };
- /**
- * The := operator overrides control and sets the value according the RHS expression.
- */
- accessor notnull String name {
- assert notempty $ : "Must have a value";
- := $.toUpperCase();
- };
- /**
- * Regular constructor, still supported.
- */
- def (String name, int age) {
- this.name = name;
- this.age = age;
- }
- /**
- *
- */
- accessor Person friend {
- set { assert $ != null : "Cannot reference self" }
- get { friend.evil ? null : friend }
- };
- def sayHello : String {
- "Hello, my name is " + name;
- }
- }
- person = Person.new::{ age = 30, name = "Mark" };
- /**
- * Throws an exception: name cannot be null.
- */
- person = Person.new::{ age = 20 };
- /**
- * Regular constructor can still be called
- */
- person = new Person("Mark", 30);
Add Comment
Please, Sign In to add comment