virtual-by-default rant

Dmitry Olshansky dmitry.olsh at gmail.com
Sun Mar 18 07:37:16 PDT 2012


On 18.03.2012 5:23, Manu wrote:
> The virtual model broken. I've complained about it lots, and people
> always say "stfu, use 'final:' at the top of your class".
>
> That sounds tolerable in theory, except there's no 'virtual' keyword to
> keep the virtual-ness of those 1-2 virtual functions I have... so it's
> no good (unless I rearrange my class, breaking the logical grouping of
> stuff in it).
> So I try that, and when I do, it complains: "Error: variable
> demu.memmap.MemMap.machine final cannot be applied to variable",
> allegedly a D1 remnant.
> So what do I do? Another workaround? Tag everything as final individually?
>
> My minimum recommendation: D needs an explicit 'virtual' keyword, and to
> fix that D1 bug, so putting final: at the top of your class works, and
> everything from there works as it should.

Following this thread and observing that you don't trust optimizer and 
compiler in many cases or have to double check them anyway, I have a 
suggestion: do virtual dispatch by hand via func-pointer table and use 
structs.

I'm serious, with a bit of metaprogramming it wouldn't be half bad, and 
as a bonus you don't have to pay for a monitor field per object as 
classes do, and in general less compiler magic to keep track of. You 
also gain the ability to fine tune their layout, the performance maniac 
side of yours must see the potential it brings :)

And since you have a few of virtuals anyway and keep them in constant 
check it should be double and be much less of a hassle then hunting down 
and second-guessing the compiler on every single step.

Bottom line thought: there is a point when a given feature doesn't bring 
significant convenience for a specific use case, it's then better to 
just stop pushing it over.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list