On 8/13/21 7:23 PM, Marcone wrote:
On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote:
On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:

Isn't there some unario operator template that I can use with lambda to handle a string literal?

So, something other than an exact "lit"[0..this.xx(..)] syntax is fine?

What didn't you like about `"Hello World!".findSplit("o")[0].writeln;` then?

What is a real example of something you want to do?

writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]);

indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal:

string x = "Hello World!";
writeln(x[x.indexOf("e")..x.indexOf("r")]);

Operator overloading is only available to custom types (structs or classes), and not to arrays.

You can create a type to do what you want.

e.g.:

```d
struct SliceByIndexOf
{
   string s;
   auto opIndex(size_t[2] idxs) {
      return SliceByIndexOf(s[ idxs[0] .. idxs[1]]);
   }
   size_t[2] opSlice(size_t dim : 0)(string s1, string s2) {
      import std.string;
      return [s.indexOf(s1), s.indexOf(s2)];
   }
    string toString() { return s; }
}

auto sbio(string s) { return SliceByIndexOf(s); }

void main()
{
    import std.stdio;
    writeln("Hello World!".sbio["e" .. "r"]); // "ello Wo"
}
```

-Steve

Reply via email to