I usually want to know if the argument was passed, not if the argument
is the default value.  With a default value, that value necessarily
has to be a legal value for the argument.  Therefore there's no way to
tell if it was omitted or if the default value was explicitly passed.

Take this function for example:

<cffunction name="test">
  <cfargument name="myArg" type="string" required="false" default="" />
  <cfif myArg EQ "">
    ...
  </cfif>
</cffunction>

The conditional will execute for both of these invocations:

test();
test("");

However, if I use this function:

<cffunction name="test">
  <cfargument name="myArg" type="string" required="false" />
  <cfif NOT structKeyExists(arguments, myArg)>
    ...
  </cfif>
</cffunction>

The conditional will only execute for the former (where the argument
wasn't passed), and it'll treat "" just like it would treat any other
passed value.

cheers,
barneyb

On Wed, 1 Dec 2004 10:27:54 -0500, Sparrow-Hood, Walter
<[EMAIL PROTECTED]> wrote:
> Barney,
>        I'm curious - why do you prefer structKeyExists() vs. setting
> default values?
> 
> Walt
> 
> 
> 
> -----Original Message-----
> From: Barney Boisvert [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, November 30, 2004 6:26 PM
> To: CF-Talk
> Subject: Re: CFC required arguments
> 
> If you use positional parameters, only parameters at the end can be
> left off.  However, you can use named parameters without using
> CFINVOKE:
> 
> <cfset o.method(name=url.name, age=url.age, ...) />
> 
> I generally prefer to not specify a default for my option arguments,
> and then use structKeyExists() against the 'arguments' struct to check
> if they were passed.  Since we don't have method overloading in CF,
> optional parameters (and their associated conditionals) are a
> necessary evil.
> 
> cheers,
> barneyb
-- 
Barney Boisvert
[EMAIL PROTECTED]
360.319.6145
http://www.barneyb.com/blog/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Silver Sponsor - New Atlanta
http://www.newatlanta.com

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:185829
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to