Wednesday, October 18, 2006

Concatenation & pass by reference

I am wondering how I will allow strings to be concatenated. The string type currently uses the + operator for this. But I am thinking whether or not to introduce an operator especially for this purpose. I suggest the ~ (tilde) as an operator (as it's not used yet and D also uses it for concatenation). An accompanying ~= operator can be introduced for concatenating to the back of an existing string.
var s : string = "How" ~ " are " ~ "you"
# s = "How are you"
s ~= " today?"
# s = "How are you today?"
This would make an interesting syntax possible for output, like C++ has the << operator. You could have an output stream that has the operators ~ and ~=. This can make the following possible:
procedure writePoint(inout s : FileOutputStream, x & y & z : real)
s ~= s ~ '(' ~ x ~ ", " ~ y ~ ", " ~ z ~ ')'
end
It's not ideal because s is still used twice. But maybe it's a start :-)

I am also thinking about pass by value or by reference for 'in' parameters. As I wrote previously, 'in' parameters cannot be changed at all by the callee. So in my opinion it would be a nice idea to use pass by reference for this. It would make 'in' parameters fit in more, because 'inout' parameters are also passed by reference and 'out' parameters use a reference to write the result directly to the location of the caller.

No comments: