Range Redesign: Empty Ranges

Atila Neves atila.neves at gmail.com
Thu Mar 7 09:11:29 UTC 2024


On Wednesday, 6 March 2024 at 13:26:14 UTC, Paul Backus wrote:
> 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
> }
> ```

Of course! I write classes so seldom I forget my own rule to add 
`final` unless I can't.

That would severely restrict class ranges though, since 
presumably the whole point is to have dynamic dispatch.


More information about the Digitalmars-d mailing list