On Sun, 7 Apr 2013 07:31:15 -0700 (PDT), ErichSt wrote:
>Can a method find out whether it is expected to return a (modified) copy
>of the receiver, or if it should directly modify the receiver?
Nope. Instead, you can make it always modify the receiver, and make your
copy *before* calling it:
Trans = Matrix~copy~~transpose
(I might also make it return the receiver, so I didn't have to worry
about remembering to use "~~".)
With routines, the distinction between function and subroutine tells the
routine whether the caller will use the result in an expression, and you
can use that to control the behavior, but I think it was originally
mostly just a matter of convenience in preventing unwanted side effects:
routine(argument) /* issues the result as a host command */
call routine argument /* doesn't */
With methods, instead the language processor just ignores the result if
it's not in an expression:
object~method /* does nothing */
(object~method) /* issues the string value as a host command */
So the caller didn't need a way to tell the method whether the result
would be used.
An interesting idea I had in thinking about this was something analogous
to the special handling of "=" at the end of method names:
object~method = argument /* calls method 'method=' if it exists */
especially as used together with the special treatment of '[]' and
parsing of expressions:
collection[index] = item /* method '[]=' sets the item */
Say collection[index] = item /* method '[]' returns the item */
If there were similar treatment of "=" before the object name:
variable = object~method /* call method '=method' if it exists */
then you could use that to trigger the copy:
::method '=transpose'
forward to (self~copy) message ('transpose')
We'd have to worry about collisions:
object1~method1 = object2~method2 /* What should happen
if object1 has both 'method1' and 'method1=' methods,
*and* object2 has both 'method2' and '=method2' methods?
*/
But really it's too late to add such a thing compatibly anyway, since
programs could already be using methods with names matching this or any
other convention we chose.
�R / Darla: Leftovers aren't the mark of a man. \
www.bestweb.net/~notr
Andrew Reid: Actually, they are, because that's how men's shirts button.