Le 2011-06-13 à 22:23, Michel Fortin a écrit :

> I'd like to ask this: would it make sense to transform Array into a template 
> and getting rid of all these casts? And if templates are still out bounds, 
> couldn't we do the same using macros? What do you think?

Various options depending on what's allowed:

What we have currently:

        // in arraytypes.h
        struct Expressions : Array {}

        // elsewhere
        Expressions exps = ...;
        Expression *e = (Expression *)exps->data[i];
        exps->data[i] = (void *)e;

With macros or templates:

        // in arraytypes.h
        typedef ArrayOf(Expression) Expressions; // with macros
        typedef Array< Expression > Expressions; // with templates

        // elsewhere
        Expressions exps = ...;
        Expression *e = exps->tdata()[i];
        exps->tdata()[i] = e;

(Note: I could use reference return and operator[] instead to make things 
tidier, but it seems DMD avoids those C++ features...)

I counted 789 instances of array->data[i] that'd need to be updated.

I'd rename the current Array struct to ArrayBase. Renaming it will make all of 
its existing uses through the code an error, pointing out each variable that 
needs to be migrated to the new type. Also, making its 'data' member protected 
will make all accesses of untyped data an error, making it easy to migrate 
those lines of code. Typed arrays would be implemented on top of ArrayBase:

Proposed macro implementation would be implemented like this:

        #define ArrayOf(TYPE) struct Array_##TYPE : ArrayBase \
        { \
            // return typed data \
            TYPE **tdata() { return (TYPE **)data; } \
        }

Proposed template implementation (just a little cleaner):

        template < class TYPE >
        struct Array : ArrayBase
        {
            // return typed data
            TYPE **tdata() { return (TYPE **)data; }
        }

What do you think? Is this change worth it? Walter, would you agree with such a 
change?


-- 
Michel Fortin
[email protected]
http://michelf.com/



_______________________________________________
dmd-internals mailing list
[email protected]
http://lists.puremagic.com/mailman/listinfo/dmd-internals

Reply via email to