Your method doesn’t return a value though. How do you propose that s
gets changed to a different class instance, merely by calling a method on it?
Yes, StringBuilder
, or some otherwise mutable type (eg a wrapper around String
) would be the only option for this. the String
class is provided by the .NET runtime, and its immutable, and there’s nothing we or you could do about it (if we wanted to ;). Same goes for String on Cocoa and Java, and on Island we have modeled String to match.
It’s also worth mentioning that from an API design perspective, if your Append
were to work as two expect, it would work orthogonally to all other existing “modification” methods on string, such as Replace
, etc, since
s.Replace("a"', "b");
will also not so what you’d “expect”, when in this mindset. Replace
doesn’t change s
either; it returns a new value (which you’d ignore). If you want to define an Append
that works in symmetry with the rest of String
, define it as
extension method String.Append(s:String): String;
begin
result := self+s;
end;
and call it as
s := "1";
s := s.Append("2");
This would work, and be more consistent. Also note that there’s already s.Concat()
, which does exactly this ;).
Yeah, I was wondering about that. I’m not sure I see a valid/reasonable/sane scenario where one would/should do this (argument for not allowing it), but of course that doesn’t mean someone hasn’t done so in existing code .
I’m still undecided on where to go with this.
Well, “legal” is, for Oxygene, what we say it is :).
I’m wondering of someone can come up with an argument for doing this (aside from “I did, and got away with it”). What can be gained by changing self, rather than using local var, that would be worth the confusion it causes?