advice needed about default arguments for a method

Stéphane Rollandin lecteur at zogotounga.net
Thu Dec 9 10:22:00 UTC 2004


hello list,


I have a design problem to expose:


1) the problem: I would like to store default arguments for some methods

for example, suppose I have Function class>>sineAmp:period: return a 
block defining a sine function with amplitude and period depending on 
its parameters. something like (never mind that period is not exactly 
the period):

----
Function class>>sineAmp: amp period: period

     ^ [[:x | (x / period) sin * amp ]] value: amp value: period
----

now I want my block to get default values in certain circumstances.
one  precise example: if I drag the above method from a Browser, I get a 
TransformMorph whose passenger is the selector #sineAmp:period:
I can then drop this morph in a function plotter morph which I wrote 
(and from where I have the possibility to define the two expected 
arguments), but I would like the dropped function to have default 
arguments to start with. Ideally those arguments should be defined 
somehow within the method source itself so that there is no ackward 
separation between the code and its associated default parameters.


2) the solution: what I did

I implemented

----
Behavior>>defaultArgumentsIfAnyFor: aSelector

   | preCoCo |

   aSelector isUnary ifTrue: [^ #()].

   preCoCo _ self precodeCommentOrInheritedCommentFor: aSelector.
   ((preCoCo beginsWith: '<') and: [preCoCo endsWith: '>'])
     ifTrue: [^ Compiler evaluate: preCoCo allButFirst allButLast].

   ^ #()
----

so that I could rewrite my example function method as:

----
Function class>>sineAmp: amp period: period

   "< #(1 1) >"

   ^ [[:x | (x / period) sin * amp ]] value: amp value: period
----

and now

	Function defaultArgumentsIfAnyFor: #sineAmp:period:

indeeds return the defaults arguments #(1 1)

...but I don't like this solution: first it supposes to introduce some 
new syntax convention in comments, second the code written in such 
comments can not be accessed by the usual introspection tools, and 
finally if the source code disappear somehow leaving only the compiled 
method then I loose my defaults arguments...



3) the question: what do you people of great wisdom think about this 
problem ? what approach would you take on this ?



thanks for your attention,

Stef



More information about the Squeak-dev mailing list