Range Redesign: Empty Ranges
Paul Backus
snarwin at gmail.com
Wed Mar 6 13:26:14 UTC 2024
On Wednesday, 6 March 2024 at 12:20:40 UTC, Atila Neves wrote:
> I like T.init being empty. I thought I'd found a clever way to
> make this work for classes but apparently this crashes, much to
> my surprise:
>
> ```d
> class Class {
> bool empty() @safe @nogc pure nothrow scope const {
> return this is null;
> }
> }
>
> Class c;
> assert(c.empty);
> ```
It crashes because it's attempting to access Class's vtable. If
you make the method final, it works:
```d
class Class {
final bool empty() {
return this is null;
}
}
void main()
{
Class c;
assert(c.empty); // ok
}
```
More information about the Digitalmars-d
mailing list