IsFunc()-like test: IsCallable()? Topic is solved

Discuss the future of the AutoHotkey language
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

IsFunc()-like test: IsCallable()?

30 Apr 2018, 07:52

Now that we have closures/lambdas (thanks again, Lexikos!), I find myself using type(obj)=='Func' or type(obj)=='Closure' a lot to test whether or not a parameter is callable with .Call(). I use this primarily when a function can take a parameter that can be either a value or a function/lambda returning a value based on other variables. Defining IsCallable(obj) in my library was trivial: (EDIT: code edited to reflect subsequent comments)

Code: Select all

IsCallable(obj) {  ; if callable, returns truthy minimum params + 1 (returns -1 for BoundFunc), else 0/false
  return (t:=type(obj))=='Closure' or t=='Func' ? obj.minparams+1 : t=='BoundFunc' ? -1 : 0
}
However, I wondered if maybe I missed a more obvious way to do this with an existing (or recently added) function? If not, since Func and Closure objects (while rightfully distinct objects/concepts) share the same properties/methods, would it make sense to have a built-in test like this that guarantees these parallel properties/methods work?
Last edited by sirksel on 04 May 2018, 07:27, edited 1 time in total.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: IsFunc()-like test: IsCallable()?

30 Apr 2018, 08:23

But you havent got bound funcs covered :P
Recommends AHK Studio
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: IsFunc()-like test: IsCallable()?

30 Apr 2018, 09:15

Great point, nnnik! No sooner did I edit/repost my code than I discovered that simply adding or t=='BoundFunc' invalidates the truthiness/minparams branch of the return, since BoundFunc doesn't support any methods other than .Call. (I think that's why I initially excluded it, now that I think about it.) It would definitely be better if BoundFunc supported .minparams, but no big deal. I do think some union test like this might make a good built-in candidate, but it would be justifiably low on the priority list. Anyone who wants it can build it.
Last edited by sirksel on 04 May 2018, 07:28, edited 1 time in total.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: IsFunc()-like test: IsCallable()?

30 Apr 2018, 11:02

Well and now there are still objects that implement .call().
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: IsFunc()-like test: IsCallable()?

30 Apr 2018, 11:14

And Im not sure that closures implement minParams and maxParams either.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: IsFunc()-like test: IsCallable()?

30 Apr 2018, 11:47

Closures have the same properties and methods as func objects.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: IsFunc()-like test: IsCallable()?  Topic is solved

03 May 2018, 03:34

Returning MinParams makes it impossible to distinguish between something which isn't callable and a function with no mandatory parameters. This is why IsFunc returns 1 + MinParams. For "is callable", it would make more sense to provide optional parameters to enable further verification; i.e. "is callable with x params". Of course, that verification may be impossible in some cases.

I have tentative plans to enable detection of methods and/or retrieving references to built-in methods. In either case, "is callable" can be inferred by "has a call method".

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 70 guests