Code: Select all
class math {
sqr[arg] {
get {
MsgBox % arg**2
}}
}
x := new math
x.sqr[3]
x.sqr(3)
Code: Select all
class math {
sqr[arg] {
get {
MsgBox % arg**2
}}
}
x := new math
x.sqr[3]
x.sqr(3)
Code: Select all
The method name can also be completely omitted, as in x[](a). Scripts can utilize this by defining a default value for the __Call meta-function's first parameter, since it is not otherise supplied with a value. Note that this differs from x.(a), which is equivalent to x[""](a). If the property or method name is omitted when invoking a COM object, its "default member" is invoked.
Code: Select all
class collection {
_items := ["placeholder1", "placeholder2", "placeholder3"]
__Call(name="", params*) {
if (name == "")
return this.items(params[1])
}
count() {
MsgBox, where the counting happens
}
sort() {
MsgBox, where the sorting happens
}
items(i) {
; some other pre-return prep stuff happens here
return this._items[i]
}
}
baubles := new collection
MsgBox % baubles.items(1) ; the way I usually do it
MsgBox % baubles[](2) ; uglier than VB's baubles(2): is this is the best way?
MsgBox % baubles.(3) ; a little prettier, but is there a downside to this? can a "" method be defined without __Call()?
Code: Select all
baubles := new collection
MsgBox % baubles[1]
baubles.count := 42
MsgBox % baubles.count
baubles.count() ; Still works.
class Collection extends Collection.Base {
__New() {
ObjRawSet(this, "_items", ["placeholder1", "placeholder2", "placeholder3"])
}
__Get(params*) {
value := this._items[params*]
; some other pre-return prep stuff happens here
return value
}
__Set(params*) {
value := params.Pop()
return this._items[params*] := value
}
class Base {
__Call(name, params*) {
if fn := Func("Obj" name)
return %fn%(this._items, params*)
}
}
count() {
MsgBox, where the counting happens
}
sort() {
MsgBox, where the sorting happens
}
_NewEnum() {
return this._items._NewEnum()
}
}
Code: Select all
class Collection extends Collection.Base {
__New() {
ObjRawSet(this, Collection, ["placeholder1", "placeholder2", "placeholder3"])
}
__Get(params*) {
value := this[Collection, params*]
; some other pre-return prep stuff happens here
return value
}
__Set(params*) {
value := params.Pop()
return this[Collection, params*] := value
}
class Base {
__Call(name, params*) {
if fn := Func("Obj" name)
return %fn%(this[Collection], params*)
}
}
count() {
MsgBox, where the counting happens
}
sort() {
MsgBox, where the sorting happens
}
_NewEnum() {
return this[Collection]._NewEnum()
}
}
Code: Select all
class Fibonacci {
_NewEnum() {
return new this.E
}
class E {
incr := 1, curr := 0
Next(ByRef n, ByRef m := "") {
n := this.curr, m := this.curr += this.incr, this.incr := n
return true
}
}
}
for n, m in Fibonacci
MsgBox % n ", " m
Esc::ExitApp
Users browsing this forum: doodles333, mikeyww and 283 guests