Question: interrogating a method's arguments

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue May 20 05:50:48 UTC 2003


"Brent Pinkney" <pinkney_b at aircom.co.za> wrote:
	I have a method which accepts 5 or so numeric arguments.  I need
	to make the sign (+/-) of each argument match the sign of the first
	non-zero argument.

Why are the arguments not passed as an array in the first place?

Let's suppose the method is

    foo: x bar: y ick: z ack: u uck: v

Now, let's get those things in an array.

	a := {x. y. z. u. v}.

We have to find the first non-zero argument.  There might not be any,
of course.

	i := a findFirst: [:x | x isZero not].

We'll take the sign as zero if there is no such x.

	sign := i = 0 ifTrue: [0] ifFalse: [(a at: i) sign].

Now to make all the signs match, we do

	a := sign >= 0 ifTrue: [a abs] ifFalse: [a abs negated].

Put that all together:

    foo: x bar: y ick: z ack: u uck: v
	|a i sign|

	a := {x. y. z. u. v}.
	i := a findFirst: [:x | x isZero not].
	sign := i = 0 ifTrue: [0] ifFalse: [(a at: i) sign].
	a := sign >= 0 ifTrue: [a abs] ifFalse: [a abs negated].
	"Proceed with the revised arguments in a."

The LEAST of your problems is getting the arguments into an array.

Note that since method arguments are read-only in Smalltalk,
there is no forgiveable way to change them; you have to proceed
with the revised argument values stored somewhere else, which
might as well be an array.

	Sure, I can beat the problem to death with a stick, but I prefer
	to elegantly decapitate it with a scimitar.
	
Yeah, well trying to change method parameters doesn't count as
"elegant" in Smalltalk, it counts as "deeply depraved and disturbed".
And using reflective machinery (thisContext arguments, should it ever
exist) just to pick up a few arguments and make an array of them is
DEFINITELY "beating the problem to death with a stick".  I mean, it's
not as if it's hard to make an Array, or anything.

	I am just amazed that no-one has ever needed to interrogate the
	arguments as an Array,

Maybe because turning the arguments into an Array is just so
forehead-smacking *easy* just the way things are already?

	Does anyone else think ContextPart>>#arguments should make it
	into 3.6 ?
	
Not me.  The problem as stated is trivially solved without it,
and it doesn't solve any other problems anyone has reported.	
	



More information about the Squeak-dev mailing list