[gameprogrammer] Re: [OT] Explicit inplace operators

On Tuesday 01 February 2005 17.14, David Olofson wrote:
[...]
>     Create a string:
>         a = "";
> 
>     Destructively append "text." to a:
>         a.+("text.");
> 
>     Destructively prepend "Add some " to a:
>         a./+("Add some ");
> 

It just dawned on me; "When doing something weird, why not just go all 
the way and do it properly?" ;-) So, here are some more syntax ideas:

    Forward:
        a += "text.";       // Of course!
        = a + "text";       // Hmm...

    Reverse:
        a /+= "Add some ";  // (Il)logical consequence?
        + a = "Add some ";  // Hard to parse? Confusing?
        a =+ "Add some ";   // Nope. Broken and confusing.
        a = "Add some " +;  // Looks like an assignment at first.
        = "Add some " + a;  // What if both operands are writable?

No... I don't like any of these reverse operator variants.

Is there *any* existing language that deals with this?

Most seem to just ignore the whole problem and provide only forward 
versions of operators, so you simply cannot express things like the 
above without resorting to something like the method call syntax I 
first proposed;

 a = "";
 a.cat("text.");
 a.insert("Add some ");

(Just using names instead of operator tokens.)

Maybe there's just no way of shoehorning what I want into a C-like 
syntax...

The C style EXPRESSION OPERATOR '=' EXPRESSION thing is perfect, but 
only handles the forward case. Problem is that I don't want to 
surprize coders by having the compiler assume that it can optimize 
this:

        a = "Add some " + a;

into this:

        a.insert("Add some ");

when it would normally be this:

        objref temp = clone a;
        temp.insert("Add some ");
        a = temp;

which is very different! In-place and copying statements need to be 
explicit, and have sufficiently different syntax to avoid confusion.


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: