Skip to content

dlib.core.oop

Timur Gafarov edited this page Apr 26, 2017 · 2 revisions

dlib.core.oop

Prototype-based OOP system for structs. Uses some template black magic to implement:

  • Multiple inheritance
  • Parametric polymorphism (struct interfaces)
  • Interface inheritance.

mixin template Inherit(PT...)

Inserts structs specified by PT type tuple as members and a dispatcher method that statically transfers any method calls and member accesses to corresponding child struct.

Free functions

  • bool implements(T, I)() - returns true if struct T has the same members and methods as struct I. This allows to use structs as static interfaces in generic code.

Usage example

struct Foo
{
    int x = 100;
    int foo() { return 11; }
}

struct Bar
{
    int y = 99;
    int bar() { return 22; }
}

struct FooBar
{
    mixin Inherit!(Foo, Bar);
}

FooBar fb;

fb.x *= 2;
fb.y = 10;

assert(fb.x == 200);
assert(fb.y == 10);

assert(fb.foo() == 11);
assert(fb.bar() == 22);
Clone this wiki locally