Closed Lexikos closed 6 years ago
Thank you sir. You must have seen my thread here: https://autohotkey.com/boards/viewtopic.php?f=37&t=51333
I see you don't like this method:
for k,v in MyClass
{
if IsObject(v) && IsFunc("MyClass." k)
See that commit above^ If neither way matters that much, then we'll just use yours
Is there any reason to use your method?
It is longer and slower. There is no guarantee that the Begin method will be named ClassName.Begin, nor that IsFunc can detect it, nor that cls.Begin
will (still) contain a reference to that method even if IsFunc succeeds.
No reason to use the other method, I was just curious. Merging yours now
IsFunc requires a function name in v2.0-a090+ and throws an exception if given an object. This was done to avoid the issue of Func vs. other callable objects. If you specifically require a Func object (i.e. with all its expected properties and methods), it is more appropriate to use an explicit type check. In general, my recommendation is to perform a type check only if absolutely necessary - so for instance, I might omit the type checks for Begin and End.
Type(x) = "Func"
is the direct replacement forIsObject(x) && IsFunc(x)
, but for more general values of x, the correct replacement would beIsObject(x) ? Type(x) = "Func" : IsFunc(x)
. I chose to use the shorter, more restrictive (object-only) form for Begin/End since it seems unlikely one would be storing function names in the test suite object vs. defining methods. The check could be omitted since the test suite object will presumably only ever contain methods and classes intended for consumption by Yunit, but I suppose it's safer to keep the check.Type(x) = "Func"
(or previouslyIsFunc(x)
) won't fully protect you against an invalid x: if x defines more mandatory parameters than you pass, your attempt to call will still throw an exception. What it will do is prevent the use of BoundFunc or user-defined objects.