• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Default or optional arguments to handlers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Default or optional arguments to handlers


  • Subject: Re: Default or optional arguments to handlers
  • From: has <email@hidden>
  • Date: Mon, 22 Apr 2002 23:46:21 +0100

Michael Sullivan wrote:

> Overloading or default arguments would really be a nice feature, don't
> you think?

I'm not sure overloading is as important to a loosely typed language like
AS as to a strongly typed one. Optional parameters would be very nice,
however. I think I've ranted a few times on how I would like to see AS
ditch [most of] its cruddy labelled parameter syntax[es] and have something
like:

--------------

on doSomething(param1:required, param2:"World")
return param1 & " " & param2
end doSomething


doSomething(param1:"Hi")
--> "Hi World"

doSomething(param2:"Jo", param1:"Hello")
-->"Hello Jo"

--------------

It's this and the lack of a Libraries folder + import command that are my
two really big bugbears when it comes to writing/using mods. They make the
whole business far more awkward than it ought to be.

Oh, and it also annoys me when I want to write nice neat wrappers for osax
calls - something that's made difficult-to-impossible by the lack of
optional parameters in AS handlers.

--

While Olof Hellman wrote:

> Trivial and elegant if you write a scriptable application. The default
>arguments can also be scriptable.

Ahh, I don't think "write a scriptable application" is valid solution to
the lack of optional parameters in AS handlers. Though it's certainly an
interesting one.:)


> In the case of an applet, using a record for the parameters is the best way
>to go.

Agreed; if positional parameters are inadequate, use a single record instead:

doSomething({param2:"Jo", param1:"Hello"})


I wouldn't use lists myself: too much room for confusion. Also, I think the
'all-or-nothing' approach works best. I've tried using stuff like:

doSomething("Hello", {})
doSomething("Hello", {param2:"Jo"})

But in practice I've found it can pretty confusing to use, even for myself.


Oh, and explain all this clearly in the documentation - I suspect many
ASers will be unfamiliar with/confused by this approach if you don't.

--

One other thought: don't get too carried away with this sort of thing. It
can get tempting to stuff lots and lots of functionality into a single
DoEverything() handler and then use tons of optional parameters to access
it all, but often the simplest approach really is best - i.e. lots of
small, simple handlers that take only the necessary parameters and perform
just a single, simple task. Not only is it simpler to code and faster to
execute, but it's a lot easier to document - which means it's also much
simpler for users to understand.

HTH

has

--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.

  • Follow-Ups:
    • Re: Default or optional arguments to handlers
      • From: Timothy Bates <email@hidden>
  • Prev by Date: Re: Number of Decimal Places
  • Next by Date: Re: OS X AND TypeText (You CAN do it with Youpi Key 1.7)
  • Previous by thread: Re: Browser Scripting
  • Next by thread: Re: Default or optional arguments to handlers
  • Index(es):
    • Date
    • Thread