#2831 closed Feature Request (Rejected)
Function Binding
Reported by: | anonymous | Owned by: | |
---|---|---|---|
Milestone: | Component: | AutoIt | |
Version: | Severity: | None | |
Keywords: | function binding | Cc: |
Description
Please create a function that can bind arguments to functions. The result of Bind(f,1,2) should be a function reference of f with an additional internal bound argument list [1, 2] (the internal list of bound arguments of normal function references would be empty). When calling $boundF(3), the call shoud be transformed to f(1,2,3).
Reason: Most functions expecting function references (_ArrayDisplay, last argument) don't call a special helper function instead of calling the passed function reference directly. That makes a general solution impossible. For each callback function a new function must be created, which is administratively expensive (compared to a simple function reference with bound arguments).
Func HandleActionCallback7 ($action, $direction) Return HandleAction ( _ $HandleActionCallback7_objectToModify, _ $action, _ $direction) EndFunc
There can be only as much callback functions as there are real functions. A function handling a callback called twice (the function, not the callback) would need at least two helper functions, and the logic to manage multiple callback functions. And if it's unknown how often the function is called...
Attachments (0)
Change History (5)
comment:1 follow-up: ↓ 2 Changed 10 years ago by jchd18
comment:2 in reply to: ↑ 1 Changed 10 years ago by anonymous
Replying to jchd18:
I'm not the only one having found your request somehow obscure.
In what way?
Can you please post a short example of working current-state code you wish to replace and then the (pseudo-)code you have in mind for performing the same process?
external-functions.au3:
; These are external functions. I don't know how they're written, and I don't want to know. Func ForEach (ByRef $array, $function) For $i = 0 To UBound ($array) - 1 $function ($array [$i]) Next EndFunc Func CreateForEachArray ($object0 = Default, $object1 = Default, $object2 = Default) Local $objects [@NumParams] For $i = 0 To @NumParams - 1 $objects [$i] = Eval ("object" & $i) Next Return $objects EndFunc
Currently:
#include "callback.au3" ; http://pastebin.com/Xqf8x54p #include "external-functions.au3" Func PrintObject ($prefix, $decimalPlaces, ByRef $object) ConsoleWrite ($prefix & " Object is at (" & Round ($object [0], $decimalPlaces) & "|" & Round ($object [1], $decimalPlaces) & ")." & @CRLF) Return $object ; Don't remove this line. EndFunc Func PrintObjects (ByRef $objects) Local Static $callCount = 0 $callCount += 1 Local $callbackIndex = ReserveCallback (CreateCallbackData (PrintObject, $callCount & ":", 1)) ForEach ($objects, $callbackFunctions [$callbackIndex]) ; In this case, I don't need to save the callback. But if I do, then I might be unable to free it later ; (custom GUICreate with ReserveCallback, but original GUIDelete without FreeCallback). FreeCallback ($callbackIndex) EndFunc Func CreateObject ($x, $y) Local $result = [$x, $y] Return $result EndFunc Local $objects = CreateForEachArray (CreateObject (10, 10), CreateObject (30, 20), CreateObject (20, 30)) PrintObjects ($objects)
Wishful thinking (and untested):
; No callback.au3, of course. ; ... Func PrintObjects (ByRef $objects) Local Static $callCount = 0 $callCount += 1 ForEach ($objects, BindFunction (PrintObject, $callCount & ":", 1)) EndFunc ; ...
comment:3 follow-ups: ↓ 4 ↓ 5 Changed 10 years ago by jchd18
- Resolution set to Rejected
- Status changed from new to closed
You really should consider using AutoItObject instead of the construction you propose. Indeed, despite having "Object" suffixed to the function names CreateObject, PrintObject, PrintObjects, none of them applies to anything generic. Those functions should really be called CreateArray1D2ElementsNumbers, PrintArray1D2ElementsNumbers, PrintArray1D2ElementsNumbers as they can only process 1D arrays with 2 elements of number type (or strings but then decimalplaces doesn't make sense).
In a true OO approach, every processed datatype or container would have a constructor, destructor, a number of commonly used methods such as Print and possibly persistent storage. Then Print(this) would invoke the Print method of whatever type (class) this is: this could be any variant or array of any dimension and content, including nested contents (map containing strings, pointers, arrays of maps, arrays of double and structures).
From this point of view the proposal is way too specific to a unique clumsy coding style and should be discarded in favor of AutoItObject.
comment:4 in reply to: ↑ 3 Changed 10 years ago by anonymous
Replying to jchd18:
You really should consider using AutoItObject instead of the construction you propose.
You misunderstood me. This has nothing to do with OOP.
In many functional programming languages, this "binding" is implicit (either implicit currying or only unary functions), other languages have special functions for that (C++ (std::bind), Javascript (Function.prototype.bind), Python, Ruby, Scala, ...).
Indeed, despite having "Object" suffixed to the function names CreateObject, PrintObject, PrintObjects, none of them applies to anything generic.
If I had called them PrintPoint etc., then this wouldn't be a generic example. My apologies for that imprecise naming.
comment:5 in reply to: ↑ 3 Changed 10 years ago by anonymous
Replying to jchd18:
This was closed because of a misunderstanding, please reopen.
Guidelines for posting comments:
- You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
- In-depth discussions should take place on the forum.
For more information see the full version of the ticket guidelines here.
I'm not the only one having found your request somehow obscure. Can you please post a short example of working current-state code you wish to replace and then the (pseudo-)code you have in mind for performing the same process?