Hi,
I wanted to make a container class that exposes its elements
using a simple "alias this", but getting weird errors:
I test with the std.algorithm.filter template function.
1. when I use "alias this" on a function that returns a slice,
making the internal array unreachable, filter just can't compile.
2. when I expose the array as it is, filter deletes the array
after it returns.
My goal is to make a class, which acts like an array, but also
having member functions to add/remove/find its items. On top of
that this class has an owner (a database table thing) too.
Example use-cases:
table.rows.add(...)
table.rows[4]
table.rows.filter!(...).map!(...)
```
import std.stdio, std.range, std.algorithm, std.uni, std.utf,
std.conv, std.typecons, std.array, std.traits, std.exception,
std.format, std.random, std.math;
class C{ //must be a class, not a struct
int[] array;
static if(0){
//BAD: filter cannot deduce from an aliased function
// that returns a nonref array
auto getArray(){ return array; }
alias getArray this;
}else{
alias array this; //this works
}
}
void main(){
auto c = new C;
c.array = [1, 2];
void wl(){ writeln("len=", c.length); }
//filtering the array explicitly: GOOD
c.array.filter!"true".each!writeln; wl;
//filtering the slice of the alias: GOOD
c.array.filter!"true".each!writeln; wl;
//filtering the alias: BAD -> erases the array
c.filter!"true".each!writeln; wl;
}
```
Thanks in advance.