Quantcast
Channel: AutoIt v3 - Developer Chat
Viewing all articles
Browse latest Browse all 750

Function Binding

$
0
0
Let's assume that there is a function that expects another function as an argument.
Func GetSortedListOfVillains (ByRef $hero, Const ByRef $sortingAlgorithmOfEvil)     ; That will take a while, so fight with swords while riding on chairs (xkcd.com/303/).     Local $villains = [$evilOverlord] ; should be global     _ArrayConcatenate ($villains, VisitGhibliHills ($hero))     _ArrayConcatenate ($villains, VisitForestOfInconvenience ($hero))     _ArrayConcatenate ($villains, VisitHillsOfModerateEvil ($hero))     _ArrayConcatenate ($villains, VisitMountDoom ($hero))     $sortingAlgorithmOfEvil ($villains)     Return $villains EndFunc
One way to use that:
Global $Sort_Arguments ; ... $Sort_Arguments = ["evilsort", $SORT_IGNORECASE] ; Let's assume that you're using a compiler which translates this into valid AutoIt. GetSortedListOfVillains ($theHero, Sort) ; ... Func Sort (ByRef $array)     If Not IsArray ($Sort_Arguments) Then Return SetError (...)     ; I changed this function yesterday. Use the specified sorting algorithm and silently ignore any other arguments.     ; Bonus points for silently changing the meaning of an argument. EndFunc
Another way:
GetSortedListOfVillains ($theHero, SpecialEvilSortUsingAUniqueVillainIdList) ; ... Func SpecialEvilSortUsingAUniqueVillainListId (ByRef $array)     ; this is the id of the list v     Return Sort ("evilsort", 16372, $array)     ; That's a magical number, but I can't pass it as an argument.     ; And I need to create a separate function for every used combination of id and sorting algorithm. EndFunc
The desired way with the hypothetical function "BindFunction":
GetSortedListOfVillains ($theHero, BindFunction (Sort, "evilsort")) ; ... Func Sort (Const ByRef $sortingAlgorithm, ByRef $array)     ; sort it     ; Time complexity of EvilSort: O((n^2)!) EndFunc
(The example code should highlight specific aspects of these approaches. That's the reason for the different signatures of the sort function.)

Is something like that possible? Do you think that it would be useful?

Viewing all articles
Browse latest Browse all 750

Latest Images

Trending Articles



Latest Images