non-member op overloads
monkyyy
crazymonkyyy at gmail.com
Thu Sep 26 03:07:12 UTC 2024
```d
import std;
auto pointertoindex(T)(T[] arr,T* p){
assert(&arr[0]<=p && &arr[$-1]>=p);
return p-&arr[0];
}
unittest{
auto foo=[1,2,3,4,5];
auto bar=&foo[0];
//pointertoindex(foo,bar).writeln;
}
auto opSlice(T)(T[] arr,T* i,T* j){
auto i_=pointertoindex(arr,i);
auto j_=pointertoindex(arr,j);
assert(j_>=i_);
return arr[i_..j_];
}
unittest{
auto somefunc(string s){
static int i=-2;
i+=5;
return &s[i];
}
auto foo="helloworld";
auto start=somefunc(foo);
auto end=somefunc(foo);
foo[start..end].writeln;
}
```
Remove the restrictions on op overloads needing to be defined in
the type. Both for a) you wrap basic types allot for one api
difference b) when you go to use a verbose op overload call call
like `T.opSlice` you need to track down every basic type and
reimpliment it or some other hack
```d
mynullable(T){
T me;
bool isnull;
int opCmp(typeof(this) a,typeof(this) b){
if( ! a.isnull && ! b.isnull){
return a.opCmp(b);
}
...
```
More information about the dip.ideas
mailing list