forked from Lexikos/winrt.ahk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload.ahk
38 lines (37 loc) · 1020 Bytes
/
overload.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
AddMethodOverloadTo(obj, name, f, name_prefix:="") {
if obj.HasOwnProp(name) {
if (pd := obj.GetOwnPropDesc(name)).HasProp('Call')
prev := pd.Call
}
if IsSet(prev) {
if !((of := prev) is OverloadedFunc) {
obj.DefineProp(name, {Call: of := OverloadedFunc()})
of.Name := name_prefix . name
of.Add(prev)
}
of.Add(f)
}
else
obj.DefineProp(name, {Call: f})
}
class OverloadedFunc {
m := Map()
Add(f) {
n := f.MinParams
Loop (f.MaxParams - n) + 1
if this.m.has(n)
throw Error("Ambiguous function overloads", -1)
else
this.m[n++] := f
}
Call(p*) {
if (f := this.m.get(p.Length, 0))
return f(p*)
else
throw Error(Format('Overloaded function "{}" does not accept {} parameters.'
, this.Name, p.Length), -1)
}
static __new() {
this.prototype.Name := ""
}
}