What is the best way to test whether or not a func...
# cfml-general
j
What is the best way to test whether or not a function argument is a callback function, as opposed to struct/array/simple? isCustomFunction doesn't work. I could use !isStruct() and !isSimpleValue() but I'm hoping there is something more elegant.
d
getFunctionList().keyExists("reMatch")
j
Thanks - is this going to pick up a callback being passed into a function?
d
Yes
j
getFunctionList()
is returning all of the built-in functions, but there is no way to see a callback
d
It's not including UDFs?
j
no - just built-in. And
isCustomFunction()
doesn't identify callbacks
d
There is always try catch
s
just an FYI,
getFunctionList
is lucee specific
i am curious why
isCustomFunction
failed, do you have a use case example?
👏🏾 1
👏 1
👀 1
d
Hey that's dope. Open source rocks
j
🙄 copy/paste bit me - thanks for the sanity check. Any idea why I'm getting this error? I've never tried executing a callback before. https://www.screencast.com/t/q0qXhWx8W2G
Copy code
if (isCustomFunction(audit.valueMap[change.key])){
										oldValue = audit.valueMap[change.key].valueMap(change.old);
										newValue = audit.valueMap[change.key].valueMap(change.new);
									} else if (isStruct(audit.valueMap)){
										oldValue = audit.valueMap[change.key][change.old].value
										newValue = a
s
shouldn’t you be calling
Copy code
audit.valueMap[change.key](change.old)
instead of
Copy code
audit.valueMap[change.key].valueMap(change.old);
or maybe
Copy code
audit.valueMap[change.key]().valueMap(change.old);
j
I need a nap 😕 2am is too early to start on a Sunday
s
ha
j
`audit.valueMap[change.key](change.old)`ftw
s
🤘
a
Why don't you use the type checking for the arguments? Something like:
doThing(required function callBack){...}
👍 2
If your
doThing
can accept a function or a struct then you getting a very confusing API which should be two functions with clearly defined arguments.
👍 1
j
this needs to be added to cfdocs...
@aliaspooryorik I understand your point. This param is built to process a structure. The only function I would pass in would be a qb or queryexecute that would use a more complex query, but return the same kind of structure.
a
I'm not seeing why you need to pass in a function or a struct? If it expects a struct then why can't you do that when you assign the argument param using something lie
valuesMap = structMap(...)
?
There are use cases for passing around functions - I get that - but being able to pass an argument as a function or a datatype just doesn't seem right.
j
I can. Honestly, it's more about I had never done it before and wanted to see how it would work 😉 I hear you on keeping the interface clean and will take it to heart
a
You can create different variants, by passing in a function to create new behaviour at runtime. This kind of thing: https://trycf.com/gist/c81d31f14d67a60e19a10f4edad59357/lucee5?theme=monokai