Re: Deafult or optional arguments to handlers
Re: Deafult or optional arguments to handlers
- Subject: Re: Deafult or optional arguments to handlers
- From: Paul Skinner <email@hidden>
- Date: Tue, 23 Apr 2002 07:05:35 -0400
I do this like so...
handlerName(parameters)
--pass in positional parameters labled parameters or a single
parameter and handle parsing the parameters in the handler. Calls with
no parameters must have some argument ie.("",{},1,pi}.
on handlerName(parameters)
if class of parameters is record then--appropriate if statement here
set requiredParameter to parameters
--or
set requiredParameter to item 1 of the parameters
--or
set requiredParameter to requiredParameter of parameters
--and/or
--appropriate optional parameter IF statement here
set optionalParameter to someItemOfTheParametersAsAppropriate
--or
set optionalParameter to "default value"
--end appropriate optional parameter IF statement here
end if
end handlerName
I use a handler wrapper that makes all my handlers have 1 parameter
like this and also provides them with an identity and documentation.
This lets me modify the handler later and retain backwards compatability
with older scripts. Passing any handler the parameter 'missing value'
will return a record of useful information about the handler and it's
usage. Linewraps are obvious
YourHandlerName(missing value)--example 'whatTheHellAreYou?' call to the
handler.
on YourHandlerName(parameters) --Only values are to be modified.
try --Encapsulating try wrapper. Modifies unhandled errors to
indicate the handler that generated them.
-->>HANDLER DOCUMENTATION:<<
set handlerName to "YourHandlerName"
if parameters is missing value then --Return the documentation
for this handler.
set handlerDescription to "YourDescriptionOfThisHandler."
set handlerExamples to
{handlerCall:"YourHandlerName(parameters)",
handlerOutput:"YourThisHandlerWillReturnThisValue"}
set handlerParameters to
{requiredParameters:{"YourRequiredParameter as YourRequiredClass"},
optionalparameters:"YourOptionalParameter as YourRequiredClass"}
set handlerRequirements to {osVersion:{"8.5", "8.5.1",
"8.6", "9", "9.1"}, asVersion:{"1.5.5", "1.6", "1.7", "1.8.1"},
osax:{"Standard Additions"}, otherHandler:{}}
set handlerVersionHistory to {{handlerVersion:"1.0 dev.",
releaseDate:3.102281366E+9, author:"YourName",
email:"email@hidden"}}
return {handlerName:handlerName,
handlerDescription:handlerDescription, handlerExamples:handlerExamples,
handlerParameters:handlerParameters,
handlerRequirements:handlerRequirements,
handlerVersionHistory:handlerVersionHistory}
end if
-->>HANDLER BODY:<<
-->>ReplaceThisLineWithActualHandlerBodyCode<<
parameters as date
-->>END HANDLER BODY:<<
on error errorMessage number errorNumber partial result errorResult
from errorFrom to ErrorTo
error (the handlerName & " " & the errorMessage) number
errorNumber partial result errorResult from errorFrom to ErrorTo
end try --End encapsulating try wrapper.
end YourHandlerName
--
Paul Skinner
On Monday, April 22, 2002, at 02:50 PM, Michael Sullivan wrote:
In writing a bunch of mods, I'm realizing that I'd really like a way to
give clients the opportunity to call a function with optional
parameters. Is there any way to do this, other than making the
parameter a list or record and then looking through it?
I'd like to able to write a function that normally takes a single number
argument, but behaves differently if there are two arguments, etc.
Is having the client pass a list or record for the multiple parameter
option, the only way?
Overloading or default arguments would really be a nice feature, don't
you think?
Michael
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.