Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # object "ex nihilo"
- var a = object {
- x: 7,
- f: func (unused) -> 99
- }
- # typeof a: {x as int; f as int -> int}
- a.x = 1
- a.f = func(y) -> this.x + y
- # type is still the same!
- a.f(5) # => 6
- # constructor, only that the members are given all in one place
- func B(s as string) {
- s = s.uppercase()
- return object {
- a: a,
- s: s,
- g: func () -> this.s + string(this.a.x)
- }
- }
- # typeof B: string -> {a as {x as int, g as int -> int}, z as string, g as () -> string }
- B("foo").g() # => "foo1"
- # inheritance, objects returned by C have a as prototype and can access its members
- func C() {
- return object <: a {
- h: func (z) -> this.f(z) + 1
- }
- }
- # typeof C: {f as int -> int} -> {f as int -> int, h as int -> int}
- # again, replacing a member (even shadowing an inherited one) doesn't need to change the type!
- c = C()
- c.f = func (unused) -> -1
- C(a).h(1) # => 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement