I have a ColdFusion function "foo" which takes three args, and the second two are optional:
我有一个ColdFusion函数“foo”,它有三个args,后两个是可选的:
<cffunction name="foo" access="public" returntype="any">
<cfargument name="arg1" type="any" required="true" />
<cfargument name="arg2" type="any" required="false" default="arg2" />
<cfargument name="arg3" type="any" required="false" default="arg3" />
...
<cfreturn whatever />
</cffunction>
I want to call foo, passing in arg1 and arg3, but leaving out arg2. I know that this is possible if I call the function using cfinvoke
, but that syntax is really verbose and complicated. I have tried these two approaches, neither works:
我想调用foo,传入arg1和arg3,但遗漏了arg2。我知道如果我使用cfinvoke调用函数,这是可能的,但是这种语法真的很冗长和复杂。我试过这两种方法,都没有用:
<cfset somevar=foo(1, arg3=3) /> <!--- gives syntax error --->
<cfset somevar=foo(1, arg3:3) /> <!--- gives syntax error --->
4 个解决方案
#1
You have to use named arguments throughout. You can't mix named and positional arguments as you can in some other languages.
你必须始终使用命名参数。您不能像在其他语言中那样混合命名和位置参数。
<cfset somevar = foo(arg1=1, arg3=3) />
#2
Or.. you can use ArgumentCollection
或者..你可以使用ArgumentCollection
In CF9 or above...
在CF9或以上......
<cfset somevar = foo(argumentCollection={arg1=1, arg3=3})>
In CF8 or above...
在CF8或以上......
<cfset args = {arg1=1, arg3=3}>
<cfset somevar = foo(argumentCollection=args)>
If CF7 or below...
如果CF7或以下......
<cfset args = structNew()>
<cfset args.arg1 = 1>
<cfset args.arg3 = 3>
<cfset somevar = foo(argumentCollection=args)>
#3
if you use named args you have to name the first too
如果你使用命名args,你也必须命名第一个
<cffunction name="foo" access="public" returntype="any">
<cfargument name="arg1" type="any" required="true" />
<cfargument name="arg2" type="any" required="false" default="arg2" />
<cfargument name="arg3" type="any" required="false" default="arg3" />
<cfreturn arg2 & " " & arg3>
</cffunction>
<cfset b = foo(arg1:1,arg3:2)>
<cfoutput>#b#</cfoutput>
#4
I too was looking for some answers as Kip posted. Following is what I implemented. Hope it could add to our chain of possible solutions. I just added <cfparam>
to the cffunction code:
我也在寻找Kip发布的答案。以下是我实施的内容。希望它可以增加我们的可能解决方案链。我刚刚将
<cffunction name="fn1" access="public" returntype="numeric">
<cfargument name="arg1" type="numeric" required="true">
<cfargument name="arg2" type="numeric" required="true">
<cfargument name="arg3" type="query" required="false">
<cfparam name="arguments.arg1" default=0>
<cfparam name="arguments.arg2" default=0>
<cfparam name="arguments.arg3" default=0>
<cfreturn arguments.arg1 + arguments.arg2 + arguments.arg3>
</cffunction>
<cfoutput>#fn1(arg1=1,arg2=2)#</cfoutput>
#1
You have to use named arguments throughout. You can't mix named and positional arguments as you can in some other languages.
你必须始终使用命名参数。您不能像在其他语言中那样混合命名和位置参数。
<cfset somevar = foo(arg1=1, arg3=3) />
#2
Or.. you can use ArgumentCollection
或者..你可以使用ArgumentCollection
In CF9 or above...
在CF9或以上......
<cfset somevar = foo(argumentCollection={arg1=1, arg3=3})>
In CF8 or above...
在CF8或以上......
<cfset args = {arg1=1, arg3=3}>
<cfset somevar = foo(argumentCollection=args)>
If CF7 or below...
如果CF7或以下......
<cfset args = structNew()>
<cfset args.arg1 = 1>
<cfset args.arg3 = 3>
<cfset somevar = foo(argumentCollection=args)>
#3
if you use named args you have to name the first too
如果你使用命名args,你也必须命名第一个
<cffunction name="foo" access="public" returntype="any">
<cfargument name="arg1" type="any" required="true" />
<cfargument name="arg2" type="any" required="false" default="arg2" />
<cfargument name="arg3" type="any" required="false" default="arg3" />
<cfreturn arg2 & " " & arg3>
</cffunction>
<cfset b = foo(arg1:1,arg3:2)>
<cfoutput>#b#</cfoutput>
#4
I too was looking for some answers as Kip posted. Following is what I implemented. Hope it could add to our chain of possible solutions. I just added <cfparam>
to the cffunction code:
我也在寻找Kip发布的答案。以下是我实施的内容。希望它可以增加我们的可能解决方案链。我刚刚将
<cffunction name="fn1" access="public" returntype="numeric">
<cfargument name="arg1" type="numeric" required="true">
<cfargument name="arg2" type="numeric" required="true">
<cfargument name="arg3" type="query" required="false">
<cfparam name="arguments.arg1" default=0>
<cfparam name="arguments.arg2" default=0>
<cfparam name="arguments.arg3" default=0>
<cfreturn arguments.arg1 + arguments.arg2 + arguments.arg3>
</cffunction>
<cfoutput>#fn1(arg1=1,arg2=2)#</cfoutput>