Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mixin functionality #590

Open
SanderMertens opened this issue Mar 7, 2017 · 0 comments
Open

Add support for mixin functionality #590

SanderMertens opened this issue Mar 7, 2017 · 0 comments

Comments

@SanderMertens
Copy link
Member

SanderMertens commented Mar 7, 2017

It is often useful to be able to share common functionality across multiple classes without defining an "as-is" relationship, as multiple inheritance is not supported in Corto.

For information on mixins and usage in other languages, see: https://en.wikipedia.org/wiki/Mixin

Mixins differ from interfaces in that common functionality is "copied into" the class, whereas an interface guarantees that the class implements a specific method. In addition, mixins will be able to contain members, whereas interfaces can only contain methods.

Because memory layout of members of a mixin cannot be guaranteed across the classes in which a mixin is used, methods implemented by the mixin require an additional level of indirection when accessing mixin members. Therefore the language binding for mixin will be different from that of regular classes.

For example, the following mixin & class definition:

mixin canEat::
    hunger: int32
    void eat(int32 amount)

class mammal: implements{canEat}:;

could result in the following C/C++ definition:

struct canEat {
    corto_int32 *hunger; // indirection, actual member is resolved at runtime
};

typedef struct mammal_s *mammal;
struct mammal_s {
    corto_int32 hunger; // inserted from mixin    
};

and the implementation could look like:

void canEat_eat(canEat this, corto_int32 amount) {
    *(this->hunger) -= amount;
}

The mixin can then be instantiated and used as follows:

mammal m = mammalCreate(10);
canEat m_canEat = canEat(m); // cast takes care of initializing mixin, no alloc needed
canEat_eat(&m); // invoke mixin method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant