Weed wrote:
Janderson пишет:
Weed wrote:
I should explain why it's important for me:

For example, I am making a matrix object(s)

It should be:
- any size
- with ability of making matrix instance of a given size in compile time.
- ability of creating matrix instance in runtime.

I have decided to make it struct because I need to create matrix
object in compile time. in real program I'll need matricies of
variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other).

I want to make matrix template and insert in it with mixin operator
several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc)

question is how those matricies can interoperate? They does not have
common parent (structures does not have inheritance) , so I can't
make common function for matricies of different sizes opAdd for
example, and have to do functions for all types of matricies.

How should I implement such a class In D properly? It possible in C++, but I like some of the language D

The classical approach is to have "helper" template functions. Essentially:

void doOpearation(mymatrix)(...)
{

}

If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template.

(I do not know, understand this long phrase on my strange English or not? : ))




Note this is compiletime polymorphisms.

Now if you want to add runtime polymorphism it is impossible to get away from a cost. You can use a wrapper class like so:

interface CommonMatrix
{
    operation()...
}

Also, interfaces can not be used with the structs


I'm not sure what you mean:

I meant this:

You have your struct:

struct Foo
{

}

Then you have your template wrapper

class DoDynamicPolymorpicStuff(T) : TheInterface
{
        T foo;

        operation()
        {
                foo;
        }
}


Its a reasonably common pattern.




class PolymorphicMatrix(mymatrix) : CommonMatrix
{
    operation()...
}

You'll probably find though that PolymorphicMartix should be some higher level concept, like car, or entity.

The great thing about these techniques is that they give a whole load more power then just using inheritance alone because you can essentially attach many different behaviors to that struct.

I hope that was helpful.

-Joel

Reply via email to