Question: interrogating a method's arguments

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue May 20 02:26:18 UTC 2003


	"Brent Pinkney" <pinkney_b at aircom.co.za> wrote:
	>Foo>>#bar: aBar baz: aBaz baq: aBaq
	>	"Negate all my arguments before continuing..."
	>	thisContext arguments do: [ :arg | arg := arg negated ].

Would this be legal?  You can't assign to a method parameter,
and I _thought_ you couldn't assign to a block parameter either.

In a Squeak 3.2 workspace, I typed

    1 to: 20 do: [:each | each := each negated]

then selected it (Cmd-A) and asked for its value (Cmd-P).
To my astonishment, the compiler did _not_ reject the attempt to
modify a block parameter.
To my further astonishment, I never got any answer.
Apparently, the code that is generated uses "each" as the loop
variable, and the assignments to each derail the incrementing code.

According to the ANSI standard, section 3.4.1, "the definitions of
methods and blocks may also include a set of _argument names_ ...
The bindings of argument names are constant bindings.  It is erroneous
for an identifier that resolves to an argument to be the target of an
assignment statement."

If there's a new compiler in 3.6 or 3.7, I do hope that it will at
least warn about assignments to block arguments, however blocks may
be implemented.

If it _were_ legal, it _still_ wouldn't do what Brent Pinkney wants,
because block and method arguments are passed by value in Smalltalk.
Even if arg := arg negated were legal, arg would be a new variable
entirely separate from the method parameter its value came from.

Of course, back comes ANSI 3.4.1:  method parameters are constant
bindings, and there is no legal way to change them.

It is difficult to imagine a method where it made sense to negate
all the arguments; if such a method exists, shouldn't it have ONE
argument, an array, in the first place?  (And anArray negated has
worked for some time in Squeak.)



More information about the Squeak-dev mailing list