Guest User

Untitled

a guest
Jan 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1.  
  2.  
  3. object Person {
  4. /**
  5. * Accessors are compact. Invariants can use assertions for validation.
  6. * Unless otherwise qualified, the body defines the setter characteristics only.
  7. */
  8. accessor notnull int age {
  9. assert $ > 0 : "Must be greater than 0";
  10. };
  11.  
  12. /**
  13. * The := operator overrides control and sets the value according the RHS expression.
  14. */
  15. accessor notnull String name {
  16. assert notempty $ : "Must have a value";
  17. := $.toUpperCase();
  18. };
  19.  
  20. /**
  21. * Regular constructor, still supported.
  22. */
  23. def (String name, int age) {
  24. this.name = name;
  25. this.age = age;
  26. }
  27.  
  28. /**
  29. *
  30. */
  31. accessor Person friend {
  32. set { assert $ != null : "Cannot reference self" }
  33. get { friend.evil ? null : friend }
  34. };
  35.  
  36. def sayHello : String {
  37. "Hello, my name is " + name;
  38. }
  39. }
  40.  
  41. person = Person.new::{ age = 30, name = "Mark" };
  42.  
  43. /**
  44. * Throws an exception: name cannot be null.
  45. */
  46. person = Person.new::{ age = 20 };
  47.  
  48. /**
  49. * Regular constructor can still be called
  50. */
  51. person = new Person("Mark", 30);
Add Comment
Please, Sign In to add comment