Vtable for virtual functions in D

Henrik henrik at nothing.com
Thu Mar 8 22:56:27 UTC 2018


On Wednesday, 7 March 2018 at 22:02:17 UTC, sarn wrote:
> On Wednesday, 7 March 2018 at 12:49:40 UTC, Guillaume Piolat 
> wrote:
>> If you know enough D maybe you can implement your own virtual 
>> functions on top of D structs. It seems no one has made it yet.
>
> When I wrote Xanthe a year ago, I rolled my own classes using 
> alias this and explicit vtables:
> https://gitlab.com/sarneaud/xanthe/blob/master/src/game/rigid_body.d#L15
> (I did this because normal D classes use the druntime library, 
> and Xanthe was an experiment in not using the D runtime at all.)
>
> Henrik: you might be interested in this post I wrote about 
> making that:
> https://theartofmachinery.com/2017/02/28/bare_metal_d.html
> NB: things are moving fast and some things have already 
> improved since then.

This is great, it is exactly what I was hoping for! The 
core.sys.posix is also pure gold. I'm obviously not the first 
person here to have an interest in this topic. Really pleasant 
reading!

In my view; the evolution of programming languages skipped at 
step after C, and forced many of us to go directly to Java, C# 
and similar languages. If D could form an natural next step after 
C, with a favorable tradeoff between memory safety and 
performance, it could really change the embedded software 
industry. C11 + Valgrind/AdressSanitizer etc are great to develop 
fast reliable software, and most universities here in Europe - 
from my experience at least - are teaching C, but not as much 
C++. I like C, but it really only dominates because everyone else 
performs so poorly in embedded/system environments.

I should probably move to the learn category for the next part, 
but I can post one example at least. It is my first naive step of 
creating a RAII struct dummy, and check my program for memory 
corruptions; two things that C cannot do. It all works good, but 
why do I have to put the @nogc on the constructor and destructor 
separately?

import std.stdio;
import core.exception;

@nogc:

struct Nice
{
     int a;
     this(int a) @nogc
     {
         this.a = a;
         puts("A");
     }

     ~this() @nogc
     {
         puts("B");
     }
}

void TestStruct()
{
     //char *c = new char();
     Nice n = Nice(55);
}

void TestIndexOutOfBounds(int i)
{
     int a[100] = void;
     a[0] = a[i];
}

void main(string[ ] args)
{
     try {
         TestStruct();
         TestIndexOutOfBounds(100);
     } catch(core.exception.RangeError e) {
         puts("Sloppy work! Enter safety mode!");
     }
}




More information about the Digitalmars-d mailing list