Slicing betterC

Oleksii al.skidan at gmail.com
Thu Sep 6 17:10:49 UTC 2018


Hi the folks,

Could you please share your wisdom with me? I wonder why the 
following code:
```
import core.stdc.stdlib;

Foo[] pool;
Foo[] foos;

auto buff = (Foo*)malloc(Foo.sizeof * 10);
pool = buff[0 .. 10];
foos = pool[0 .. 0 ];

// Now let's allocate a Foo:
Foo* allocatedFoo;
if (foos.length < foos.capacity) {    // <= Error: TypeInfo 
cannot be used with -betterC
   allocatedFoo = foos[0 .. $ + 1];    // <= Error: TypeInfo 
cannot be used with -betterC
}
```
fails to compile because of `foos.capacity` and `foos[0 .. $ + 
1]`. Why do these two innocent looking expressions require 
TypeInfo? Aren't slices basically fat pointers with internal 
structure that looks like this:
```
struct Slice(T) {
   size_t capacity;
   size_t size;
   T*     memory;
}
```
?

It's weird that `TypeInfo` (being a run-time and reflection 
specific thing) is required in this particular case. Shouldn't 
static type checking be enough for all that?

Thanks in advance,
--
Oleksii


More information about the Digitalmars-d-learn mailing list