www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct inheritance need?

reply Weed <resume755 mail.ru> writes:
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
Dec 16 2008
next sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
MjAwOC8xMi8xNiBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gSSBzaG91bGQgZXhwbGFpbiB3
aHkgaXQncyBpbXBvcnRhbnQgZm9yIG1lOgo+Cj4gRm9yIGV4YW1wbGUsIEkgYW0gbWFraW5nIGEg
bWF0cml4IG9iamVjdChzKQo+Cj4gSXQgc2hvdWxkIGJlOgo+IC0gYW55IHNpemUKPiAtIHdpdGgg
YWJpbGl0eSBvZiBtYWtpbmcgbWF0cml4IGluc3RhbmNlIG9mIGEgZ2l2ZW4gc2l6ZSBpbiBjb21w
aWxlIHRpbWUuCj4gLSBhYmlsaXR5IG9mIGNyZWF0aW5nIG1hdHJpeCBpbnN0YW5jZSBpbiBydW50
aW1lLgo+Cj4gSSBoYXZlIGRlY2lkZWQgdG8gbWFrZSBpdCBzdHJ1Y3QgYmVjYXVzZSBJIG5lZWQg
dG8gY3JlYXRlIG1hdHJpeAo+IG9iamVjdCBpbiBjb21waWxlIHRpbWUuIGluIHJlYWwgcHJvZ3Jh
bSBJJ2xsIG5lZWQgbWF0cmljaWVzIG9mCj4gdmFyaWFibGUgc2l6ZSBhbmQgM8gxLCAzyDMsIDbI
NiwgM8g2IHNpemVzIChhbmQgbWF5IGJlIG90aGVyKS4KPgo+IEkgd2FudCB0byBtYWtlIG1hdHJp
eCB0ZW1wbGF0ZSBhbmQgaW5zZXJ0IGluIGl0IHdpdGggbWl4aW4gb3BlcmF0b3IKPiBzZXZlcmFs
IHN0cnVjdHVyZXMgb2YgZGlmZmVyZW50IHNpemVzIChhbmQgdGhpcyBzdHJ1Y3RzIGFyZSBub3Qg
c3RvcmUgaGlzCj4gZGltZW5zaW9ucyBpbiBlYWNoIGluc3RhbmNlLCBvZiBjb3Vyc2U6IHZhcmlh
YmxlcyB3aWR0aCBhbmQgaGVpZ2h0IGRlY2xhcmVkCj4gYXMgaW52YXJpYW50KS4gQnkgZG9pbmcg
dGhpcyBJJ2xsIGdldCBzZXZlcmFsIGRpZmZlcmVudCBzdHJ1Y3R1cmVzCj4gIChtYXRyaXhfZHlu
YW1pYywgbWF0cml4M3gzLCBtYXRyaXg2eDYgZXRjKQo+Cj4gcXVlc3Rpb24gaXMgaG93IHRob3Nl
IG1hdHJpY2llcyBjYW4gaW50ZXJvcGVyYXRlPyBUaGV5IGRvZXMgbm90IGhhdmUKPiBjb21tb24g
cGFyZW50IChzdHJ1Y3R1cmVzIGRvZXMgbm90IGhhdmUgaW5oZXJpdGFuY2UpICwgc28gSSBjYW4n
dAo+IG1ha2UgY29tbW9uIGZ1bmN0aW9uIGZvciBtYXRyaWNpZXMgb2YgZGlmZmVyZW50IHNpemVz
IG9wQWRkIGZvcgo+IGV4YW1wbGUsIGFuZCBoYXZlIHRvIGRvIGZ1bmN0aW9ucyBmb3IgYWxsIHR5
cGVzIG9mIG1hdHJpY2llcy4KPgo+IEhvdyBzaG91bGQgSSBpbXBsZW1lbnQgc3VjaCBhIGNsYXNz
IEluIEQgcHJvcGVybHk/Cj4gSXQgcG9zc2libGUgaW4gQysrLCBidXQgSSBsaWtlIHNvbWUgb2Yg
dGhlIGxhbmd1YWdlIEQKPgoKSGVyZSdzIG9uZSB3YXk6Cmh0dHA6Ly93d3cuZHNvdXJjZS5vcmcv
cHJvamVjdHMvb3Blbm1lc2hkL2Jyb3dzZXIvdHJ1bmsvTGluQWxnL2xpbmFsZy9NYXRyaXhULmQK
Ci0tYmIK
Dec 16 2008
parent Weed <resume755 mail.ru> writes:
Bill Baxter пишет:
 2008/12/16 Weed <resume755 mail.ru>:
 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
Here's one way: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d --bb
necessarily need the ability to change the size of the matrix together with a static matrix it can be done using two types of matrices with a single parent.
Dec 16 2008
prev sibling next sibling parent reply Weed <resume755 mail.ru> writes:
Weed пишет:
 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
I think could help a static creation objects of classes (including compile-time creation), but it is also impossible now. For example: MatrixClass a(3,2); static MatrixClass a(3,2); Planned in the future to implement inheritance of structs or the static creation of classes?
Dec 16 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Weed:
 Planned in the future to implement inheritance of structs or the static 
 creation of classes?
Inheritance of structs: I think it's not planned. Structs in D are meant to be used for different things than classes. Yet, as time passes structs are gaining more power: you can't believe that very recently they have gained constructors/destructors too in D2. Probably in a system language conceptual purity isn't much appreciated :-) Static creation of classes (I think you mean creation of objects): it sounds like an interesting thing, I know of a system language that allows the creation of objects only at compile-time, and at compile-time it also performs several space optimizations among such objects (and such space optimizations often improve running speed a little). Bye, bearophile
Dec 16 2008
parent reply Weed <resume755 mail.ru> writes:
bearophile пишет:
 Weed:
 Planned in the future to implement inheritance of structs or the 
static creation of classes?
 Inheritance of structs: I think it's not planned. Structs in D are
meant to be used for different things than classes.
 Yet, as time passes structs are gaining more power: you can't believe
that very recently they have gained constructors/destructors too in D2. Probably in a system language conceptual purity isn't much appreciated :-)

I believe that the opportunity to place an object in memory, stack or 
heap is more important than the struggle against "splicing".



allocation of memory by a small object + its using and the use of a 

a specialist in the design of compilers :))

 Static creation of classes (I think you mean creation of objects): it
 sounds like an interesting thing, I know of a system language that
 allows the creation of objects only at compile-time, and at
 compile-time it also performs several space optimizations among such
 objects (and such space optimizations often improve running speed a
 little).
And in fact we come to making structs and classes similar except that classes can not be assigned by value. Such an option I like.
Dec 16 2008
parent reply Yigal Chripun <yigal100 gmail.com> writes:
Weed wrote:
 bearophile пишет:
  > Weed:
  >> Planned in the future to implement inheritance of structs or the
 static creation of classes?
  >
  > Inheritance of structs: I think it's not planned. Structs in D are
 meant to be used for different things than classes.
  > Yet, as time passes structs are gaining more power: you can't believe
  >
 that very recently they have gained constructors/destructors too in D2.
 Probably in a system language conceptual purity isn't much appreciated :-)
  >

 I believe that the opportunity to place an object in memory, stack or
 heap is more important than the struggle against "splicing".



 allocation of memory by a small object + its using and the use of a

 a specialist in the design of compilers :))

  > Static creation of classes (I think you mean creation of objects): it
  > sounds like an interesting thing, I know of a system language that
  > allows the creation of objects only at compile-time, and at
  > compile-time it also performs several space optimizations among such
  > objects (and such space optimizations often improve running speed a
  > little).

 And in fact we come to making structs and classes similar except that
 classes can not be assigned by value.

 Such an option I like.
If I understand you correctly - I think you confuse here two separate and orthogonal issues. 1) struct vs. class 2) memory allocation What D tries to do is to provide types with value semantics via structs and types with reference semantics _and_polymorphism_ via classes. IMO C++ is a prime example how to not design a language and the slicing problem is a good example of that. value types should not have polymorphism whatsoever as is done in D. memory allocation is orthogonal to this: class C {...} Struct S {...} auto c1 = new C; // classes default to heap alloc scope c2 = new C; // c2 is on stack S s1; // structs default to stack auto s2 = new S; // s2 is S* (heap alloc) I think Andrei said he wants to make changes to "scope", that is IIRC. struct inheritance, if it gets implemented in D, should express IMO semantics similar to concepts as in C++0x, not provide polymorphism for value types which is clearly a mistake. (also this will remove the need for typedefs, isn't it?)
Dec 16 2008
parent reply Weed <resume755 mail.ru> writes:
Yigal Chripun пишет:

 
 If I understand you correctly - I think you confuse here two separate 
 and orthogonal issues.
 1) struct vs. class
 2) memory allocation
 
 What D tries to do is to provide types with value semantics via structs 
 and types with reference semantics _and_polymorphism_ via classes.
 IMO C++ is a prime example how to not design a language and the slicing 
 problem is a good example of that. value types should not have 
 polymorphism whatsoever as is done in D.
 
 memory allocation is orthogonal to this:
 class C {...}
 Struct S {...}
 
 auto c1 = new C; // classes default to heap alloc
 scope c2 = new C; // c2 is on stack
 S s1; // structs default to stack
 auto s2 = new S; // s2 is S* (heap alloc)
 
 I think Andrei said he wants to make changes to "scope", that is IIRC.
 
What will change?
 struct inheritance, if it gets implemented in D, should express IMO 
 semantics similar to concepts as in C++0x, not provide polymorphism for 
 value types which is clearly a mistake. (also this will remove the need 
 for typedefs, isn't it?)
I agree. In my case I chose to structure rather than a class because it can be initialized at compile time. But now I thing must be allowed to deploy class in the default data segment. And add the possibility of creating a object of class at compile time. Nothing prevents this change?
Dec 16 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 I agree.
 In my case I chose to structure rather than a class because it can be 
 initialized at compile time.
 
 But now I thing must be allowed to deploy class in the default data 
 segment. And add the possibility of creating a object of class at 
 compile time.
If you want to use full blown OOP, class is your choise. There are static constructors for static initialization.
Dec 17 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 I agree.
 In my case I chose to structure rather than a class because it can be 
 initialized at compile time.

 But now I thing must be allowed to deploy class in the default data 
 segment. And add the possibility of creating a object of class at 
 compile time.
If you want to use full blown OOP, class is your choise. There are static constructors for static initialization.
There does not need a static initialization of static members of the class. There must be able to create objects of classes at compile time.
Dec 17 2008
next sibling parent Weed <resume755 mail.ru> writes:
Weed пишет:
 Kagamin пишет:
 Weed Wrote:

 I agree.
 In my case I chose to structure rather than a class because it can be 
 initialized at compile time.

 But now I thing must be allowed to deploy class in the default data 
 segment. And add the possibility of creating a object of class at 
 compile time.
If you want to use full blown OOP, class is your choise. There are static constructors for static initialization.
There does not need a static initialization of static members of the class. There must be able to create objects of classes at compile time.
=========== Now what? How to initiate the procedure for adding this feature?
Dec 17 2008
prev sibling parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 There does not need a static initialization of static members of the 
 class. There must be able to create objects of classes at compile time.
Well, these objects should be placed in some static variables, right? Static variables are initialized with static constructors. Global variables are static members of module, wich is similar to class and also has static constructor.
Dec 18 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 There does not need a static initialization of static members of the 
 class. There must be able to create objects of classes at compile time.
Well, these objects should be placed in some static variables, right?
Yes
 Static variables are initialized with static constructors.
Yes, static variables of class are initialized with static constructor of class.
 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
Dec 18 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
So your problem is solved.
Dec 18 2008
next sibling parent Kagamin <spam here.lot> writes:
Well, the wish to place an object at specific location is not a problem, it's a
wish.
Dec 18 2008
prev sibling parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
So your problem is solved.
If everything was so simple! :) Once again: the static constructor of class NOT constructs an object. He just fills the static variables of class. These variables are common to all objects of class.
Dec 18 2008
next sibling parent Weed <resume755 mail.ru> writes:
Weed пишет:
 Kagamin пишет:
 Weed Wrote:

 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
So your problem is solved.
If everything was so simple! :) Once again: the static constructor of class NOT constructs an object. He just fills the static variables of class. These variables are common to all objects of class.
*It* just fills the static variables of class. These variables are common to all objects of class. sorry:)
Dec 18 2008
prev sibling parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 Kagamin пишет:
 Weed Wrote:
 
 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
So your problem is solved.
If everything was so simple! :) Once again: the static constructor of class NOT constructs an object.
Static constructor can execute any valid D statements including construction of objects. This works: --- import std.stdio; class Matrix { int i; this(int j) { i=j; } } Matrix m; static this() { m=new Matrix(7); } void main() { writeln(m.i); } ---
Dec 18 2008
next sibling parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 Kagamin пишет:
 Weed Wrote:

 Global variables are static members of module, wich is similar to
 class and also has static constructor.
So what?
So your problem is solved.
If everything was so simple! :) Once again: the static constructor of class NOT constructs an object.
Static constructor can execute any valid D statements including construction of objects.
The static constructor does not take *this*. Therefore, it can not create instance of object. In your case it initiates the external variable.
 This works:
 ---
 import std.stdio;
 
 class Matrix
 {
 	int i;
 
 	this(int j)
 	{
 		i=j;
 	}
 }
 
 Matrix m;
 
 static this()
 {
 	m=new Matrix(7);
 }
 
 void main()
 {
     writeln(m.i);
 }
 ---
Dec 18 2008
parent Kagamin <spam here.lot> writes:
Weed Wrote:

 Therefore, it can not create instance of object.
You have a trouble with terminology here. In my example m=new Matrix(7); creates an instance of Matrix class.
Dec 18 2008
prev sibling next sibling parent reply naryl <cyNOSPAM ngs.ru> writes:
Kagamin Wrote:
 Static constructor can execute any valid D statements including construction
of objects.
 This works:
 ---
 import std.stdio;
 
 class Matrix
 {
 	int i;
 
 	this(int j)
 	{
 		i=j;
 	}
 }
 
 Matrix m;
 
 static this()
 {
 	m=new Matrix(7);
 }
 
 void main()
 {
     writeln(m.i);
 }
 ---
I think the problem with that is that if you have a lot of functions every one of which needs a static instance of the same class you'll need to maintain a list of static objects in the static constructor. Weed wants every object to be declared in the function where it is needed.
Dec 18 2008
parent Kagamin <spam here.lot> writes:
naryl Wrote:

 Weed wants every object to be declared in the function where it is needed.
Haa?.. void main() { Matrix m=new Matrix(7); writeln(m.i); }
Dec 18 2008
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 18 Dec 2008 07:24:34 -0500, Kagamin wrote:

 Static constructor can execute any valid D statements including construction
of objects.
A static constructor (also known as the Module constructor) executes at program run-time and not at program compile-time. I think Weed wants the ability to have the compiler build class objects at compile-time such that when a program first starts running, the objects are already fully formed in RAM just waiting to be used. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 18 2008
next sibling parent Weed <resume755 mail.ru> writes:
Derek Parnell пишет:
 On Thu, 18 Dec 2008 07:24:34 -0500, Kagamin wrote:
 
 Static constructor can execute any valid D statements including construction
of objects.
A static constructor (also known as the Module constructor) executes at program run-time and not at program compile-time. I think Weed wants the ability to have the compiler build class objects at compile-time such that when a program first starts running, the objects are already fully formed in RAM just waiting to be used.
It is exactly
Dec 18 2008
prev sibling parent reply Kagamin <spam here.lot> writes:
Derek Parnell Wrote:

 A static constructor (also known as the Module constructor) executes at
 program run-time and not at program compile-time.
So do C++ static object constructors. Though C++ has syntax sugar, which helps writing declaration and initialization at the same place.
Dec 19 2008
next sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Fri, 19 Dec 2008 04:07:40 -0500, Kagamin wrote:

 Derek Parnell Wrote:
 
 A static constructor (also known as the Module constructor) executes at
 program run-time and not at program compile-time.
So do C++ static object constructors. Though C++ has syntax sugar, which helps writing declaration and initialization at the same place.
C++ static object constructors execute at run time except for trivial cases. Essentially C++ provides a sugar for static Foo var; static this() { var = new Foo(); } I wonder if D could do this as well.
Dec 19 2008
parent reply Kagamin <spam here.lot> writes:
Sergey Gromov Wrote:

 C++ static object constructors execute at run time except for trivial
 cases.
Although I think it's not guaranteed to work this way and compiler decides when to execute constructor. So code should be ready for run-time evaluation. And as code evolves those constructors can switch back and forth between ct and rt evaluation. ps I know a C++ programmer, he wanted to control manually, in what order static object constructors are called :)
Dec 21 2008
parent Sergey Gromov <snake.scaly gmail.com> writes:
Sun, 21 Dec 2008 12:04:42 -0500, Kagamin wrote:

 Sergey Gromov Wrote:
 
 C++ static object constructors execute at run time except for trivial
 cases.
Although I think it's not guaranteed to work this way and compiler decides when to execute constructor. So code should be ready for run-time evaluation. And as code evolves those constructors can switch back and forth between ct and rt evaluation. ps I know a C++ programmer, he wanted to control manually, in what order static object constructors are called :)
Actually, static constructors are *guaranteed* to run at run time, before main(). Compiler *may* optimize them out and convert into initialized DATA if there are no side effects, that is, if optimized program works exactly as if all static ctors were executed at run time. C++ guarantees that static ctors are executed in order they appear in a module. There is no guarantee about the order in which different modules are initialized though.
Dec 22 2008
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 19 Dec 2008 04:07:40 -0500, Kagamin wrote:

 Derek Parnell Wrote:
 
 A static constructor (also known as the Module constructor) executes at
 program run-time and not at program compile-time.
So do C++ static object constructors. Though C++ has syntax sugar, which helps writing declaration and initialization at the same place.
That's nice I suppose, but I don't think that this is what Weed is talking about. I think he wants to have some objects constructed at compile-time. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 19 2008
parent reply Kagamin <spam here.lot> writes:
Derek Parnell Wrote:

 I think he wants to have some objects constructed at compile-time. 
Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put.
Dec 21 2008
next sibling parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Derek Parnell Wrote:
 
 I think he wants to have some objects constructed at compile-time. 
Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put.
please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945
Dec 21 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 Sure he wants. From my point of view, this technique is supposed to be a means
to solve some problem rather than problem itself. But this problem was not put.
please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945
The problem of matrix of arbitrary size is solved by size-agnostic base class whose interface is very similar to that of dynamic matrix.
Dec 21 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 Sure he wants. From my point of view, this technique is supposed to be a means
to solve some problem rather than problem itself. But this problem was not put.
please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945
The problem of matrix of arbitrary size is solved by size-agnostic base class whose interface is very similar to that of dynamic matrix.
If 2 predetermined matrix certain size multiplied, the produced matrix must be a certain size rather than dynamic.
Dec 21 2008
parent reply Weed <resume755 mail.ru> writes:
Weed пишет:
 Kagamin пишет:
 Weed Wrote:

 Sure he wants. From my point of view, this technique is supposed to 
 be a means to solve some problem rather than problem itself. But 
 this problem was not put.
please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmar .D&article_id=80945
The problem of matrix of arbitrary size is solved by size-agnostic base class whose interface is very similar to that of dynamic matrix.
If 2 predetermined matrix certain size multiplied, the produced matrix must be a certain size rather than dynamic.
...and a matrix of static size in the same used in other objects - matrix functional mixed into this objects by mixin. that is, suppose that after some action should get a matrix matrix3x1 You can not assign it to "pixel" struct, you will receive matrix3x1 and make copy of "pixel".
Dec 21 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 that is, suppose that after some action should get a matrix matrix3x1
Well... if you want to template every piece of your code, this can cause disaster, so I think, this is not very good design. For example, multiplication method will be duplicated N*N*N times for all possible matrix size combinations. I'd prefer run-time checks, though templates can be used for sure.
Dec 22 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 that is, suppose that after some action should get a matrix matrix3x1
Well... if you want to template every piece of your code, this can cause disaster, so I think, this is not very good design. For example, multiplication method will be duplicated N*N*N times for all possible matrix size combinations.
Only for really used combinations (i am using "duck typing" inside templates). In any case, unused functions in the resulting file is not included.
 I'd prefer run-time checks, though
 templates can be used for sure.
This problem would help solve the availability of inheritance for structs or compile-time creation of class instances. But now the compiler can identify duplicate parts of the code, working with the same types
Dec 22 2008
next sibling parent Kagamin <spam here.lot> writes:
Weed Wrote:

 I'd prefer run-time checks, though
 templates can be used for sure.
This problem would help solve the availability of inheritance for structs or compile-time creation of class instances.
I see no way, how it can help.
Dec 23 2008
prev sibling parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 I'd prefer run-time checks, though
 templates can be used for sure.
This problem would help solve the availability of inheritance for structs or compile-time creation of class instances.
And I see no problem. Absence of compile-time object creation doesn't prevent you from using templates.
Dec 23 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 I'd prefer run-time checks, though
 templates can be used for sure.
This problem would help solve the availability of inheritance for structs or compile-time creation of class instances.
And I see no problem. Absence of compile-time object creation doesn't prevent you from using templates.
The problem is not in use templates. Templates are implementing some of the functionality of 2 types of structures matrices (normal and dynamic). But the structures do not inherit, then to add functionality matrix to other entities ( "pixel", "image" etc) sites will have their list in a template return type. For example: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d see template MultReturnType(ArgT) It contain list of all types (MatrixT and VectorT) for return. Will it add types of "image" and "pixel" and still others if needed. This is as good as manually implement a new object model. It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time.
Dec 23 2008
next sibling parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 The problem is not in use templates.
 
 Templates are implementing some of the functionality of 2 types of 
 structures matrices (normal and dynamic). But the structures do not 
 inherit, then to add functionality matrix to other entities ( "pixel", 
 "image" etc) sites will have their list in a template return type.
If structs don't suit you, don't use them. Classes are better suited for OOP as I said long ago and continue repeating it over and over.
 It contain list of all types (MatrixT and VectorT) for return. Will it 
 add types of "image" and "pixel" and still others if needed. This is as 
 good as manually implement a new object model.
I'm sure any properly formalized problem is solvable. All you need is proper formalization and some design work.
 It is not necessary to suggest to wrap up "pixel" in a class - then it 
 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Dec 23 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 The problem is not in use templates.

 Templates are implementing some of the functionality of 2 types of 
 structures matrices (normal and dynamic). But the structures do not 
 inherit, then to add functionality matrix to other entities ( "pixel", 
 "image" etc) sites will have their list in a template return type.
If structs don't suit you, don't use them. Classes are better suited for OOP as I said long ago and continue repeating it over and over.
 It contain list of all types (MatrixT and VectorT) for return. Will it 
 add types of "image" and "pixel" and still others if needed. This is as 
 good as manually implement a new object model.
I'm sure any properly formalized problem is solvable. All you need is proper formalization and some design work.
 It is not necessary to suggest to wrap up "pixel" in a class - then it 
 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Sometimes it is the only way to avoid a large number of global ad
Dec 23 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 It is not necessary to suggest to wrap up "pixel" in a class - then it 
 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Sometimes it is the only way to avoid a large number of global ad
In D module variables can be protected by access modifiers and become module-local.
Dec 24 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 It is not necessary to suggest to wrap up "pixel" in a class - then it 
 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Sometimes it is the only way to avoid a large number of global ad
In D module variables can be protected by access modifiers and become module-local.
Module full of mathematics turns into horror quite quickly. I have checked this:)
Dec 24 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Sometimes it is the only way to avoid a large number of global ad
In D module variables can be protected by access modifiers and become module-local.
Module full of mathematics turns into horror quite quickly. I have checked this:)
If you have troubles with understanding big modules, you can split them into smaller modules, which will be easier to understand for sure.
Dec 24 2008
parent reply Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 too cannot be initialized in a compile time.
Sure it can't. Does it cause that big problems?
Sometimes it is the only way to avoid a large number of global ad
In D module variables can be protected by access modifiers and become module-local.
Module full of mathematics turns into horror quite quickly. I have checked this:)
If you have troubles with understanding big modules, you can split them into smaller modules, which will be easier to understand for sure.
If you do not want to initialize repeatedly matrix inside the sub, which often cause each other, must be static matrices or declared as global (in relation to these procedures). You agree with that? Do you propose through mixin divide the code into parts? You do not think that it is better to add to language that would give 100% to use the opportunity of calculations at compile time. This greatly simplify the code and perfectly and logically fit into the language.
Dec 24 2008
parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 If you do not want to initialize repeatedly matrix inside the sub, which 
 often cause each other, must be static matrices or declared as global 
 (in relation to these procedures). You agree with that?
What's problem? If you want static or global variables, you have them in D.
 Do you propose through mixin divide the code into parts?
Well, why mixin? Just create different modules and place code according to its purpose to these modules.
 You do not think that it is better to add to language that would give 
 100% to use the opportunity of calculations at compile time. This 
 greatly simplify the code and perfectly and logically fit into the language.
Compile-time evaluation is just an optimization technique, which may or may not be applied without any change to source code, so it doesn't affect source code in any way, it just can't. In C++ you can't tell whether the code executes at compile time, as as of standard, it's completely up to the compiler to optimize code generation.
Dec 25 2008
next sibling parent Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 If you do not want to initialize repeatedly matrix inside the sub, which 
 often cause each other, must be static matrices or declared as global 
 (in relation to these procedures). You agree with that?
What's problem? If you want static or global variables, you have them in D.
 Do you propose through mixin divide the code into parts?
Well, why mixin? Just create different modules and place code according to its purpose to these modules.
 You do not think that it is better to add to language that would give 
 100% to use the opportunity of calculations at compile time. This 
 greatly simplify the code and perfectly and logically fit into the language.
Compile-time evaluation is just an optimization technique, which may or may not be applied without any change to source code, so it doesn't affect source code in any way, it just can't. In C++ you can't tell whether the code executes at compile time, as as of standard, it's completely up to the compiler to optimize code generation.
Yeah, right. I propose that it be expanded to optimize the static arrays and classes.
Dec 25 2008
prev sibling parent Weed <resume755 mail.ru> writes:
Kagamin пишет:
 Weed Wrote:
 
 If you do not want to initialize repeatedly matrix inside the sub, which 
 often cause each other, must be static matrices or declared as global 
 (in relation to these procedures). You agree with that?
What's problem? If you want static or global variables, you have them in D.
 Do you propose through mixin divide the code into parts?
Well, why mixin? Just create different modules and place code according to its purpose to these modules.
 You do not think that it is better to add to language that would give 
 100% to use the opportunity of calculations at compile time. This 
 greatly simplify the code and perfectly and logically fit into the language.
Compile-time evaluation is just an optimization technique, which may or may not be applied without any change to source code,
Incidentally, this is incorrect. In the event that it is impossible to calculate the static expression compiler immediately gives an error rather than moves calculation to run-time.
Dec 25 2008
prev sibling parent Weed <resume755 mail.ru> writes:
Weed пишет:
 Kagamin пишет:
 Weed Wrote:

 I'd prefer run-time checks, though
 templates can be used for sure.
This problem would help solve the availability of inheritance for structs or compile-time creation of class instances.
And I see no problem. Absence of compile-time object creation doesn't prevent you from using templates.
The problem is not in use templates. Templates are implementing some of the functionality of 2 types of structures matrices (normal and dynamic). But the structures do not inherit, then to add functionality matrix to other entities ( "pixel", "image" etc) sites will have their list in a template return type. For example: http://www.dsource.org/projects/openmeshd/browser/trunk/Lin lg/linalg/MatrixT.d see template MultReturnType(ArgT) It contain list of all types (MatrixT and VectorT) for return. Will it add types of "image" and "pixel" and still others if needed. This is as good as manually implement a new object model. It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time.
In fact, the minimum number of matrices 3: dynamic fixed-size fixed-size static The last 2 are different way of storing components - "fixed-size static" has dynamic array (because compile-time assignment is now does not assign anything to static array) and "fixed-size" it with a static array with size calculated in the template. I will be happy if someone tells a mistake in the design of my idea :)
Dec 24 2008
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 21 Dec 2008 19:34:52 +0300, Kagamin <spam here.lot> wrote:

 Derek Parnell Wrote:

 I think he wants to have some objects constructed at compile-time.
Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put.
Ok, here is a problem out of my head: Let's say we have a GUI application with Widgets and WidgetFactories. Each widget is initialized with a *valid* pointer to some WidgetFactory (a dummy one, perhaps) to avoid "if (factory !is null) factory.doSomething();" checks (for example). Our first try: import std.stdio; struct WidgetFactory { int someParameterValue = 42; } WidgetFactory defaultFactory = { 14 }; class Widget { WidgetFactory* factory = &defaultFactory; } void main() { Widget w = new Widget(); writefln(w.factory.someParameterValue); // prints 14 } It works, because memory for global structs is reserved at compile time and pointer to it is also a known compile-time expression. Let's go further and see if we can replace a struct with a class instance: WidgetFactory defaultFactory; class Widget { WidgetFactory factory = defaultFactory; } This code compiles but doesn't work as we need. Another try: WidgetFactory defaultFactory; static this() { defaultFactory = new StubFactory(); } class Widget { WidgetFactory* factory = &defaultFactory; } Works as we need but we now have to dereference a pointer twice. A better solution would be to have the following: WidgetFactory defaultFactory = new StubFactory(); class Widget { WidgetFactory factory = defaultFactory; } A memory for 'defaultFactory' is reserved at compile-time whereas a ctor is ran at run-time (similar to "static this" construct). In fact, allowing reference type to created at compile time would eliminate most of the needs for static this. The following construct: Foo foo; static this() { foo = new Foo(); } could be replaced with Foo foo = new Foo(); which behaves just the same but is shorter, nicier, easier to write and read (undestand).
Dec 21 2008
parent reply Kagamin <spam here.lot> writes:
Denis Koroskin Wrote:

 class Widget
 {
     WidgetFactory* factory = &defaultFactory;
 }
 
 void main()
 {
     Widget w = new Widget();
     writefln(w.factory.someParameterValue); // prints 14
 }
You initialize member field here. It's usually done in instance constructor. class Widget { WidgetFactory factory; this(){ factory = defaultFactory; } }
Dec 22 2008
parent reply Denis Koroskin <2korden gmail.com> writes:
Kagamin Wrote:

 Denis Koroskin Wrote:
 
 class Widget
 {
     WidgetFactory* factory = &defaultFactory;
 }
 
 void main()
 {
     Widget w = new Widget();
     writefln(w.factory.someParameterValue); // prints 14
 }
You initialize member field here. It's usually done in instance constructor. class Widget { WidgetFactory factory; this(){ factory = defaultFactory; } }
I don't agree with you. If so, then why we have the following syntax allowed: class Foo { int i = 42; } if we could just use class Foo { this() { i = 42; } } ? Imagine you have lots of ctors (with different arguments), should you put i = 42; into every one? Or move it into some initialize() method? Don't you think it is too verbose?
Dec 22 2008
parent Kagamin <spam here.lot> writes:
Denis Koroskin Wrote:

 I don't agree with you. If so, then why we have the following syntax allowed:
 
 class Foo
 {
     int i = 42;
 }
It fits well into .init feature. If you want to split constructor, some subtle bugs can arise. For example one programmer stumbled into such bug in Java: base class constructor was called, it called virtual method, overriden in derived class, this method assigned an object to a field, then base class constructor returned and derived field initializers were called and they assigned null to that field, so object ended up with null in the field.
Dec 23 2008
prev sibling parent reply Janderson <ask me.com> writes:
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)(...) { } 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()... } 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
Dec 17 2008
parent reply Weed <resume755 mail.ru> writes:
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
 
 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
Dec 17 2008
next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 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? : ))
What operations do you need at compile time and what operations do you need at runtime?
Dec 17 2008
parent reply Weed <resume755 mail.ru> writes:
Christopher Wright пишет:
 Weed wrote:
 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? : ))
What operations do you need at compile time and what operations do you need at runtime?
Compile-time creation an object of class or (most likely wrong) struct inheritance. I have prepared a distinct feature request and send it later
Dec 17 2008
next sibling parent Weed <resume755 mail.ru> writes:
Weed пишет:
 Christopher Wright пишет:
 Weed wrote:
 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? : ))
What operations do you need at compile time and what operations do you need at runtime?
Compile-time creation an object of class or (most likely wrong) struct inheritance. I have prepared a distinct feature request and send it later
You can read this in the digitalmars.D, topic: Feature request: Deploying a class instance in the default data segment
Dec 17 2008
prev sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 Compile-time creation an object of class or (most likely wrong) struct 
 inheritance.
 
 I have prepared a distinct feature request and send it later
You aren't providing a use case, though. Why not show an example (actual code) of what you would do if you had this ability?
Dec 18 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Thu, 18 Dec 2008 18:36:07 -0500, Christopher Wright wrote:

 Weed wrote:
 Compile-time creation an object of class or (most likely wrong) struct 
 inheritance.
 
 I have prepared a distinct feature request and send it later
You aren't providing a use case, though. Why not show an example (actual code) of what you would do if you had this ability?
static invariant Identity = new Matrix([[1,0,0], [0,1,0], [0,0,1]]);
Dec 19 2008
parent reply Christopher Wright <dhasenan gmail.com> writes:
Sergey Gromov wrote:
 Thu, 18 Dec 2008 18:36:07 -0500, Christopher Wright wrote:
 
 Weed wrote:
 Compile-time creation an object of class or (most likely wrong) struct 
 inheritance.

 I have prepared a distinct feature request and send it later
You aren't providing a use case, though. Why not show an example (actual code) of what you would do if you had this ability?
static invariant Identity = new Matrix([[1,0,0], [0,1,0], [0,0,1]]);
Okay, do you want to access that matrix in a template? Is there a reason it can't be a struct?
Dec 20 2008
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 20 Dec 2008 16:46:44 +0300, Christopher Wright  
<dhasenan gmail.com> wrote:

 Sergey Gromov wrote:
 Thu, 18 Dec 2008 18:36:07 -0500, Christopher Wright wrote:

 Weed wrote:
 Compile-time creation an object of class or (most likely wrong)  
 struct inheritance.

 I have prepared a distinct feature request and send it later
You aren't providing a use case, though. Why not show an example (actual code) of what you would do if you had this ability?
static invariant Identity = new Matrix([[1,0,0], [0,1,0], [0,0,1]]);
Okay, do you want to access that matrix in a template? Is there a reason it can't be a struct?
Let's say, there is an IMatrix interface and its implementations, such as SparseMatrix, RowMajor/ColumnMajorMatrix etc. class IdentityMatrix : IMatrix { public float at(int row, int col) { return (row == col) ? 1 : 0; } }
Dec 20 2008
parent Christopher Wright <dhasenan gmail.com> writes:
Denis Koroskin wrote:
 On Sat, 20 Dec 2008 16:46:44 +0300, Christopher Wright 
 <dhasenan gmail.com> wrote:
 
 Sergey Gromov wrote:
 Thu, 18 Dec 2008 18:36:07 -0500, Christopher Wright wrote:

 Weed wrote:
 Compile-time creation an object of class or (most likely wrong) 
 struct inheritance.

 I have prepared a distinct feature request and send it later
You aren't providing a use case, though. Why not show an example (actual code) of what you would do if you had this ability?
static invariant Identity = new Matrix([[1,0,0], [0,1,0], [0,0,1]]);
Okay, do you want to access that matrix in a template? Is there a reason it can't be a struct?
Let's say, there is an IMatrix interface and its implementations, such as SparseMatrix, RowMajor/ColumnMajorMatrix etc. class IdentityMatrix : IMatrix { public float at(int row, int col) { return (row == col) ? 1 : 0; } }
Score! We finally have a use case! I thought there would be a reasonable use case, but I couldn't come up with one off the top of my head. The workaround would be to use a struct at compile time and convert to an appropriate class when necessary. This is a rather ugly workaround; it takes a fair bit of time at runtime and increases executable sizes (I don't think you could do a sparse matrix at compile time, for instance -- you would need dynamic allocation for that).
Dec 20 2008
prev sibling parent reply Janderson <ask me.com> writes:
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
Dec 17 2008
parent Weed <resume755 mail.ru> writes:
Janderson пишет:
 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.
How do you implement matrix data of this class in compile-time in the case of matrix is fixed-sized? Whether you can start immediately after the program began to operate this class matrix as if it was created at compile time?
 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
Dec 17 2008