CF 101.1. Within a method, I want to only honor th...
# cfml-general
d
CF 101.1. Within a method, I want to only honor the arguments that were actually declared, not random stuff passed in. I can build a struct out of the name field values in this.someMethod.getMetadata().parameters, then anything that exists in that struct is a declared argument. I can wrap that in a (private probably) method, getDeclaredArgs(required function method) for clarity. Is there a more elegant, less-code way to do this? Not that this is a big pile of code, just wondering. And before folks jump on me for code that may pass random-ish arguments, there's Reasons :)
w
you can get the argument values that were passed in vs those arguments with default values by iterating the arguments as an array rather than a struct. i have code for it, sec
Copy code
The Arguments scope is special, as it is BOTHa struct AND an array

When you dump the arguments scope, it always appears as a struct

However, you can also treat it like an array in order to find out which arguments are actually being passed in

For example, if you do arraylen(arguments) you will get how many arguments were passed in during the method call (vs how many may be defined as arguments, which may or may not have default values)

Additionally, you could do something like this to actually see which arguments were passed in:
	for(var i = 1; i <= arraylen(arguments); i++) {	
	writeoutput("argument #i#: " & arguments[i] & "<br>"); 
}

IsArray(arguments) will always return false, however
a
Can you expand more on what "to honor" means in this context? It might impact how the question is answered.
w
you should be able to achieve what you want if i understand correctly
the above was copy/paste from a ppt presentation i did a few years ago
you can also getMetaData() on the object that contains the method (or the method itself) and look at the defined arguments from that if you need to compare what's defined in the method signature vs inbound values
if what' you're doing is super meaningful for all functions in an object, i'd suggest enumerating the arguments for each method once when the object is instantiated and store defined args in the variables scope to avoid the metadata cost each time
sorry for rambling on this so much, but another thought. you could enforce this directly in the method itself with something like the following, but it may feel like 'duplicating' values, even though i think it's sound:
Copy code
public any function doWhatever(
	required string username, 
	numeric age = 0, 
	string location
) {

	var args = {
		"username" : "", 
		"age" : 0, 
		"location" : "Earth"
	};

	for( var a in args ) {
		if( arguments.keyExists(a) ) {
			args[a] = arguments[a];
		}
	}

	// now only those args that you care about are available in the args struct with any values that may have been passed in

}
i guess technically you don't even need to define arguments for the method example above, but if you want type checking then clearly you do