[v2-beta7] Possible BoundFunc MinParams bug Topic is solved

Report problems with documented functionality
Descolada
Posts: 1138
Joined: 23 Dec 2021, 02:30

[v2-beta7] Possible BoundFunc MinParams bug

Post by Descolada » 16 Aug 2022, 23:28

Hello,

Code: Select all

f := TheFunc.Bind(42)
MsgBox(f.MinParams)
f("")

TheFunc(param1, param2) {
    MsgBox("TheFunc: " param1)
}

Expected behaviour would be for f.MinParams == 1, but the result is that f.MinParams == 0. Calling f() throws a "Missing a required parameter" error. Am I missing something here?
I discovered this while trying to pass a BoundFunc into CallbackCreate, but it seems it isn't working without specifying the required param count, which is supplied by func.MinParams...

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [v2-beta7] Possible BoundFunc MinParams bug  Topic is solved

Post by swagfag » 17 Aug 2022, 04:40

yeah, u missed the part where it says
Other properties and methods are inherited from Func, but do not reflect the properties of the target function or method (which is not required to be implemented as a function). The BoundFunc acts as an anonymous variadic function with no other formal parameters, similar to the fat arrow function below:

Code: Select all

Func_Bind(fn, bound_args*) {
    return (args*) => (args.InsertAt(1, bound_args*), fn(args*))
}
u can recover the target function and do some math to figure what its "new MinParams" count should be, and then override BoundFunc's methods to use that procedure instead:

Code: Select all

#Requires AutoHotkey v2.0-beta.4

TheFunc(param1, param2) {
    MsgBox("TheFunc: " param1)
}

f := TheFunc.Bind(42)
pf := ObjPtr(f)
BoundParamsArray := ObjFromPtrAddRef(NumGet(pf, (A_PtrSize = 4) ? 44 : 72, 'Ptr'))
WrappedTheFunc := ObjFromPtrAddRef(NumGet(pf, (A_PtrSize = 4) ? 36 : 56, 'Ptr'))

actuallyBoundParamsCount := 0
Loop BoundParamsArray.Length
	if BoundParamsArray.Has(A_Index) ; handles func.Bind(, , , 42) type scenarios
		++actuallyBoundParamsCount

actualMinParams := WrappedTheFunc.MinParams - actuallyBoundParamsCount

MsgBox actualMinParams
this isnt an exhaustive implementation, u still need to handle BoundFuncs of BoundFuncs, methods, closures and probably other edge cases i havent yet thought of

Post Reply

Return to “Bug Reports”