Using colons with named arguments in ColdFusion

I saw this sample code in a post and wanted to know what the colon syntax did. I was looking for documents, but I could not find information about this:

weather.subscribe(observer: application.observers.currentConditions);

I know that we can use the colon in CF9 for ternary operators:

result = (condition) ? true : false;

But in this case, it looks like it is used to provide named arguments; so what is he doing there?

+7
source share
3 answers

<cfset result = obj.func(arg:value,thing:42) /> I looked at it and went to blink, blink ... This may not be right! You cannot use colons for named arguments! Uh, right? Well, apparently you can.

http://corfield.org/blog/post.cfm/Learn_something_new_every_day_named_arguments

+10
source

Yes, you are allowed to use both options. I think this is a matter of preference. You can even mix.

Try this and see, mock some test function:

 <cffunction name="testFunction" returntype="void" hint="I just spit out the arguments I get"> <cfdump var="#arguments#" label="arguments"> </cffunction> <cfset testFunction(arg1:"hello",arg2:"world") /> <cfset testFunction(arg1="hello",arg2="world") /> <cfset testFunction(arg1:"I can mix",arg2="my named argument syntax") /> 

Personally, I prefer = for named arguments. You may also notice that if you use IntelliJ IDEA to develop ColdFusion, then they do not recognize the colon syntax, so for better analysis you would like to use the = syntax. I can not speak for other IDEs

0
source

Looks like a typo to me. In ColdFusion, you must use an equal sign ( = ), not a colon, to use named arguments.

Your example:

 weather.subscribe(observer = application.observers.currentConditions); 
-one
source

All Articles