Advertisement
Guest User

Fictional static prototypes

a guest
Jul 23rd, 2011
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # object "ex nihilo"
  2. var a = object {
  3.     x: 7,
  4.     f: func (unused) -> 99
  5. }
  6. # typeof a: {x as int; f as int -> int}
  7. a.x = 1
  8. a.f = func(y) -> this.x + y
  9. # type is still the same!
  10. a.f(5) # => 6
  11.  
  12. # constructor, only that the members are given all in one place
  13. func B(s as string) {
  14.     s = s.uppercase()
  15.     return object {
  16.         a: a,
  17.         s: s,
  18.         g: func () -> this.s + string(this.a.x)
  19.     }
  20. }
  21. # typeof B: string -> {a as {x as int, g as int -> int}, z as string, g as () -> string }
  22. B("foo").g() # => "foo1"
  23.  
  24. # inheritance, objects returned by C have a as prototype and can access its members
  25. func C() {
  26.     return object <: a {
  27.         h: func (z) -> this.f(z) + 1
  28.     }
  29. }
  30. # typeof C: {f as int -> int} -> {f as int -> int, h as int -> int}
  31. # again, replacing a member (even shadowing an inherited one) doesn't need to change the type!
  32. c = C()
  33. c.f = func (unused) -> -1
  34. C(a).h(1) # => 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement