On Wed, Sep 10, 2014 at 12:53 PM, Nick Wellnhofer <[email protected]> wrote:
> Why don’t we simply initialize the itable array with NULLs and have a
> function like
>
> Futzer*
> Futzer_from_obj(Obj* obj) {
> if (!obj->klass->itables[Futzer_INTERFACE_ID]) {
> // Build the interface table and store it in itables array.
> // Throw if the class doesn’t satisfy the interface.
> }
> return (Futzer*)obj;
> }
+1, this is a better solution. It should allow NULLs, though.
if (obj != NULL && !obj->klass->itables[Futzer_INTERFACE_ID]) {
I feel like the name could be shorter. How about generating a `toCLASSNAME`
macro for every Clownfish interface (and every class) which performs a safe
runtime cast? We'd have to accept `void*` which is somewhat dangerous,
but oh well.
Futzer *futzer = toFUTZER(obj);
This would allow us to eliminate DOWNCAST.
Foo *foo = (Foo*)DOWNCAST(obj, FOO);
Foo *foo = toFOO(obj);
If we add a NOTNULL, it would also allow us to replace CERTIFY, at the cost of
slightly degraded error messages on NULL values (because type information
which is available to CERTIFY would not be available to NOTNULL).
Foo *foo = (Foo*)CERTIFY(obj, FOO);
Foo *foo = toFOO(NOTNULL(obj));
Marvin Humphrey