Does Coldfusion support dynamic arguments?

There is a *args convention in python, I'm wondering if CF9 supports something like that.

Here is a python example

 >>> def func(*args): for a in args: print a, "is a quality argument" >>> func(1, 2, 3) 1 is a quality argument 2 is a quality argument 3 is a quality argument >>> 
+6
source share
3 answers

Yes, CFML supports dynamic arguments as long as it supports user-defined functions.

All arguments explicitly given or passed without definition exist in the Arguments area.

The "Arguments" area can be considered both an array and a structure (key / value).


Here is the closest equivalent to your example using the script syntax:

 function func() { for (a in arguments) WriteOutput(arguments[a] & "is a quality argument"); } 

Note that a in this example is the key name, not the value, therefore arguments[a] .

To be processed as code, the above script must either be in the <cfscript> .. </cfscript> , or alternatively inside the component { .. } block inside the .cfc file.


Here are a couple of tag versions, the first equivalent to the for / in loop:

 <cffunction name="func"> <cfloop item="a" collection=#Arguments#> <cfoutput>#Arguments[a]# is a quality argument</cfoutput> </cfloop> </cffunction> 


And this allows you to directly access the value (i.e. a is the value here):

 <cffunction name="func"> <cfloop index="a" array=#Arguments#> <cfoutput>#a# is a quality argument</cfoutput> </cfloop> </cffunction> 


In Railel * CFML, this last example can be expressed in a script as:

 function func() { loop index="a" array=Arguments { WriteOutput(a & 'is a quality argument'); } } 

* Railo is one of two Open Source alternatives for Adobe ColdFusion, the other is Open BlueDragon .

+10
source

Yes, arguments are passed to functions as an array called "arguments". Alternatively, you can pass an array named "argumentCollection" to a function.

 public void function myFunct(){ var myVar = ""; if(arrayLen(arguments)){ myVar = arguments[1]; } } 

Calling functions with dynamic arguments:

 myFunc("hello","world"); OR myFunc(argumentCollection=["Hello","World"]); 

In addition, you can expand the arguments to have named arguments and arguments that are behind named arguments:

 public void function myFunction(String arg1){ var secondArgument = ""; if(arraylen(arguments) > 1){ secondArgument = arguments[2]; } } myFunction("Hello","World"); 
+2
source

Depending on how the function is called, the arguments may be contained in a structure with numeric keys related to their position (index based on 1) or in a structure with the argument name as the key.

if you call a function like this:

 func(1, "foo"); 

then the arguments will be available as arguments [1] and arguments [2]

if the function is called passing in named arguments or, for example, using the Collection argument;

 func(foo=1, bar="foo"); 

then the arguments are available as arguments ["foo"] and arguments ["bar"]

0
source

Source: https://habr.com/ru/post/901446/


All Articles