SafeCall — Call any function with any amount of arguments

Post your working scripts, libraries and tools.
Michaohneel
Posts: 7
Joined: 07 Feb 2021, 16:37
Contact:

SafeCall — Call any function with any amount of arguments

13 Oct 2021, 14:47

SafeCall allows you to safely call any function, regardless of the number of arguments you are passing or the parameters it expects.

This is most useful with callbacks, to which you might want to provide data via arguments.
To do this, either the callback is forced to define corresponding parameters, or the caller has to check how many arguments the callback expects.
This short function makes this easy, by checking the amount of arguments passed against the function's expected parameters.
It's nothing crazy, but it can save you some work sometimes.

GitHub

Note that this does not perform type checking, functions can still throw parameter errors if you pass them arguments of invalid types.

Everyone is free to use this in whatever way they like, as detailed at the bottom of the Read-Me file.

Thank you for reading and have a nice day :)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: SafeCall — Call any function with any amount of arguments

13 Oct 2021, 20:56

ive read the post, ive read the code, ive read the tests, ive read the markdown, and im still not sure what the use-case for this is.

if im gonna be invoking a callback, im invoking it with precisely as many parameters as my library-code intends to provide - nothing less, nothing more. if parameter are being provided, then it stands to reason that theyre supposed to be used for something.
if the client-code doesnt care about some(or all) of the provided parameters(for whatever reason), then they should discard any parameters that they dont care about(ie lone, unnamed variadic * at the end of the parameter list). then the intent is as clear as can be

this seems like its bringing the old v1 call-anything-anytime-withanyparams behavior, so maybe it should have been more aptly named v1GarbageCall instead :lol:

essentially, having the library-code make that choice instead-of/for the client-code is inviting more potential for misuse(since ure bypassing whatever parameter checking v2 would have automatically done for u)... idk
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: SafeCall — Call any function with any amount of arguments

23 Oct 2021, 13:27

swagfag wrote:
13 Oct 2021, 20:56
ive read the post, ive read the code, ive read the tests, ive read the markdown, and im still not sure what the use-case for this is.
I wrote a similar function myself. In my case I have functions which accept callbacks as arguments, and the callbacks are allowed a flexible number of parameters. I found it handy to write this utility function so that I could supply my callbacks willy nilly, then have the calling function just call them with the maximum number of arguments every time, which get truncated based on how many arguments the callback formally expects. (I believe my version is literally called "TruncateArguments")

basically, JavaScript array helper functions. Maybe one callback only needs the value, next one wants the index and the array too, etc.. It's lame to account for the full argument list every time I want to pass a callback. Sometimes sloppy is nice
Adventurer
Posts: 23
Joined: 18 Apr 2019, 06:24

Re: SafeCall — Call any function with any amount of arguments

27 Oct 2021, 18:54

Very useful for a script I'm working on, thank you.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 20 guests