On Friday, 6 August 2021 at 16:32:59 UTC, james.p.leblanc wrote:
On Friday, 6 August 2021 at 11:58:59 UTC, Paul Backus wrote:
struct MySlice(T, size_t maxLength)
{
    private T[] payload;
    invariant(payload.length <= maxLength);
    this(T[] slice) { payload = slice; }
    T opIndex(size_t i) { return payload[i]; }
    // etc.
}
```

Paul,

Thanks very much for your reply.  I understand only
a fraction of the suggested solution.  I will need to
digest this a bit, and do some more background reading
on templates.

My initial, naive attempts to use this in a simple main()
were unsuccessful.  I'll keep plugging at it ...

Best Regards,
James

If you want something even simpler, maybe this could help:

```d
void setCapacity(int/*or whatever type you want*/ a[], ulong capacity){
    assert(capacity < 100/*whatever you want to set*/);
    a.length = capacity;
}

void main(){
     int[] arr;
     setCapacity(arr, 50);//works :D
     //setCapacity(arr, 1000);// fails :(
}
```
Of course, this won't work if you want to resize the capacity natively, ie, by using ```.length``` directly; then you will have to use Paul's solution.

If you still have difficulties, please ping.

Also, if you're completely new, please try the book "programming in D", it is a marvellous resource.
link:
http://ddili.org/ders/d.en/

Reply via email to