string is rarely useful as a function argument

Adam D. Ruppe destructionator at gmail.com
Wed Dec 28 11:48:27 PST 2011


On Wednesday, 28 December 2011 at 19:30:04 UTC, Andrei 
Alexandrescu wrote:
> Implementation would entail a change in the compiler.

I don't think I agree. Wouldn't something like this work?

===

struct string {
        immutable(char)[] rep;
        alias rep this;
        auto opAssign(immutable(char)[] rhs) {
                rep = rhs;
                return this;
        }

        this(immutable(char)[] rhs) {
                rep = rhs;
        }
        // disable these here so it isn't passed on to .rep
        @disable void opSlice(){  assert(0);  };
        @disable size_t length() {  assert(0);  };
}

===

I did some quick tests and the basics seemed ok:

/* paste impl from above */

import std.string : replace;

void main() {
        string a = "test"; // works

        a = a.replace("test", "mang"); // works
        // a = a[0..1]; // correctly fails to compile
        assert(0, a); // works
}



More information about the Digitalmars-d mailing list