Copy link to clipboard
Copied
Hello, all,
This question is most likely for the Adobe Dev Team to answer, if any of them have time. But if anyone else knows the answer, please respond.
I was doing some research into functions and arguments, and on the Adobe Help Center page for functions and arguments it states that if a function is created using CFFUNCTION, then an 'empty slot' is automatically created for each argument, in case any of them are optional. However, if a function is created within CFSCRIPT, then no 'empty slots' are created, and if an argument is not supplied it will error out as undefined.
So, I'm curious as to why the two work differently from each other. If, within a CFSCRIPT environment, I want arguments to be optional I have to param them. But not in a CFFUNTION context.
Just curious.
V/r,
^ _ ^
Copy link to clipboard
Copied
How about showing some simple code that demonstrates your point, rather than relying solely on the docs' assertion (or on us to create and confirm it). And do indicate in what version/s you confirm it.
You can use Cffiddle.org or trycf.com to demo things, even offering a link to a saved cffiddle demo.
I say all this to help, not to criticize.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Charlie+Arehart wrote
I say all this to help, not to criticize.
Oh, I know, Charlie. I'll use trycf.com because it offers what we are using, CF11, where cffiddle.org only offers 2016 and 2018. I'll post a link as soon as I have it done.
V/r,
^ _ ^
Copy link to clipboard
Copied
Okay, no I won't. SMH.. can't log on to save it on cftry.org, so I'll just post the code here and anyone who wants to can copy/paste it into whichever testing environment they wish.
In the following code, if you run it as is it will throw an error (arg3 is not defined); however, if you uncomment the code on line 9 and run it, it will work. Outside of the CFSCRIPT tag, the code runs fine as is, even though the third call omits the third argument:
<cfscript>
try{
strct = {
title1: function() {writeOutput('Title One: ' & arguments['arg3'] & '<br />');},
title2: function() {writeOutput('Title Two: ' & arguments['arg2'] & '<br />');},
dflt: function() {writeOutput('Title Three: ' & arguments['arg1'] & '<br />');}
};
function alertIt(doWhat,arg1,arg2,arg3){
//param name="arg3" default="";
var thisAction = structKeyExists(strct,doWhat)
? doWhat
: "dflt" ;
var func = strct[thisAction];
func(arg1 = arg1, arg2 = arg2, arg3 = arg3);
}
}catch(any e){
writeDump(e);
}
alertIt('title1','First','Second','Third');
alertIt('title2','Fourth','Fifth','Sixth');
alertIt('asdf','Seventh','Eighth');
</cfscript>
<cftry>
<cffunction name="alertTwo" access="public" output="yes">
<cfsavecontent variable="dumpThis"> | |
#arguments[1]#, #arguments[2]#, #arguments[3]#<br /> | |
</cfsavecontent> |
<cfreturn dumpThis />
</cffunction>
<cfcatch>
<cfdump var="#cfcatch#" />
</cfcatch>
</cftry>
<cfoutput>
#alertTwo('one','two','three')#
#alertTwo('four','five','six')#
#alertTwo('seven','eight')#
</cfoutput>
Copy link to clipboard
Copied
This doesn't really answer your question, but under the covers I think they're quite different. When you use CFFUNCTION, I seem to recall that the function actually gets compiled into a separate class. I don't think that happens when you create a function within CFSCRIPT. So I guess I'm surprised that they're as similar as they are, rather than that they work differently.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
So Wolf, that's an awfully convoluted set of code (defining functions in struct keys, and later calling them), and then too the two different methods (the cffunction-based one, and the cfscript-based one) do not do the same thing. More important (to demonstrating your concern), they don't take the same args, and most important, in the body of the script-one you refer to the args using named args while in the other you refer to them using unnamed args.
In fact, if you change that "failing" line to this:
func(arg1 = arg1, arg2 = arg2, arg3 = arguments[3]);
then your error goes away without the need of uncommenting the param. I haven't the bandwidth now to connect the dots as to why that failed without it, but perhaps this will be a clue to you or Dave or others to see the complete explanation.
And if not, I think you'd help your cause (helping others help you) by creating a far simpler example that demonstrates this contention of yours (and demonstrating that in the fewest lines possible). I know that takes work and time, which you may not have either.
Finally, Dave, in running the code to generate the error (I did it at cffiddle), we can see in the error output the reference to the class (file name of the class) being created for that specific script-based method, so that's not it. 🙂
Copy link to clipboard
Copied
Charlie,
The 'convoluted' code is, I'm guessing, my dispatch table that I'm trying to use in place of switch/case statements, and I have not been able to get a dispatch table in CFCODE to work; I've only gotten them to work inside a CFSCRIPT tag.
And, yes, I did use different nomenclature between the two, but I will give it another shot on cftry.com as you suggested.
V/r,
^ _ ^
Copy link to clipboard
Copied
For some reason, I could access cftry.com this morning, and now it is being blocked by BlueCoat (category: none). Welcome to my world. SMH.
So, I tried as you suggested and on cffiddle.org using CF2016 (the oldest they offer), it did work without uncommenting the code. But I'm going to wait until BlueCoat reviews cftry.com to get it unblocked and try it with CF11 (which is what we are using) just to see if there is a difference.
Still, the code I originally provided does match up with what was stated on Adobe Help Center. Which just adds to the confusion.
V/r,
^ _ ^
Copy link to clipboard
Copied
Okay, there is not enough caffeine in the world, right now, and the used KVM that I replaced my defective KVM with is really screwing with my keyboard, right now, so I'm just going to walk away from my desk for a few minutes.
Copy link to clipboard
Copied
WolfShade wrote
... if a function is created using CFFUNCTION, then an 'empty slot' is automatically created for each argument, in case any of them are optional. However, if a function is created within CFSCRIPT, then no 'empty slots' are created, and if an argument is not supplied it will error out as undefined.
I really don't understand. Please provide some code to illustrate.