www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Structure's inheritance

reply "Temtaime" <temtaime gmail.com> writes:
Hello to all !
I'm surprised that there is no structure's inheritance.

There is a problem:
I want to make Matrix MxN class. I also want to make child 
classes Square Matrix and Vector from it with additional 
functions.
I don't want use "class" cause of "new" overhead. Std typecons's 
Scoped - workaround with many restrictions, so it isn't true 
solution.

I'm tried mixin template but there is no luck. I don't know how 
to declare multiplication operator for example.

Any suggestions ?
Regards.
May 12 2013
next sibling parent "evilrat" <evilrat666 gmail.com> writes:
On Sunday, 12 May 2013 at 11:21:16 UTC, Temtaime wrote:
 Hello to all !
 I'm surprised that there is no structure's inheritance.

 There is a problem:
 I want to make Matrix MxN class. I also want to make child 
 classes Square Matrix and Vector from it with additional 
 functions.
 I don't want use "class" cause of "new" overhead. Std 
 typecons's Scoped - workaround with many restrictions, so it 
 isn't true solution.

 I'm tried mixin template but there is no luck. I don't know how 
 to declare multiplication operator for example.

 Any suggestions ?
 Regards.
opertor overloading described here http://dlang.org/operatoroverloading.html#Binary what are you trying to do that requires struct inheritance?
May 12 2013
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 12 May 2013 at 11:21:16 UTC, Temtaime wrote:
 Hello to all !
 I'm surprised that there is no structure's inheritance.

 There is a problem:
 I want to make Matrix MxN class. I also want to make child 
 classes Square Matrix and Vector from it with additional 
 functions.
 I don't want use "class" cause of "new" overhead. Std 
 typecons's Scoped - workaround with many restrictions, so it 
 isn't true solution.

 I'm tried mixin template but there is no luck. I don't know how 
 to declare multiplication operator for example.

 Any suggestions ?
 Regards.
You could make the base struct a member of the child struct and refer with alias this Base; to the base: [code] import std.stdio; struct A { public: int id; } struct B { public: A hidden_a; alias hidden_a this; int age; this(int id, int age) { this.age = age; this.id = id; } } void main() { B b = B(42, 23); writefln("Person #%d is %d years old.", b.id, b.age); } [/code]
May 12 2013
prev sibling next sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Sunday, 12 May 2013 at 11:21:16 UTC, Temtaime wrote:
 Hello to all !
 I'm surprised that there is no structure's inheritance.
This was done by Walter Bright deliberately because he believes that polymorphism does not play well with automaic lifetime.
 There is a problem:
 I want to make Matrix MxN class. I also want to make child 
 classes Square Matrix and Vector from it with additional 
 functions.
 I don't want use "class" cause of "new" overhead. Std 
 typecons's Scoped - workaround with many restrictions, so it 
 isn't true solution.

 I'm tried mixin template but there is no luck. I don't know how 
 to declare multiplication operator for example.

 Any suggestions ?
 Regards.
You can place base struct instance inside nested and use alias this. Note that currently multiple alias this are not supported. Also note that you cannot override functions because there are no virtual table for structs.
May 12 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Sunday, 12 May 2013 at 11:56:53 UTC, Maxim Fomin wrote:
 You can place base struct instance inside nested and use alias
 this. Note that currently multiple alias this are not supported.
 Also note that you cannot override functions because there are 
 no
 virtual table for structs.
why multiple alias this not supported? i know it was broken in 2.060 but with 2.061 it was fixed right?
May 12 2013
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
12-May-2013 16:00, evilrat пишет:
 On Sunday, 12 May 2013 at 11:56:53 UTC, Maxim Fomin wrote:
 You can place base struct instance inside nested and use alias
 this. Note that currently multiple alias this are not supported.
 Also note that you cannot override functions because there are no
 virtual table for structs.
why multiple alias this not supported? i know it was broken in 2.060 but with 2.061 it was fixed right?
It's not even on the horizon. It must be single alias this that finally started working (around that time ~ 2.061). -- Dmitry Olshansky
May 12 2013
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-05-12, 14:00, evilrat wrote:

 On Sunday, 12 May 2013 at 11:56:53 UTC, Maxim Fomin wrote:
 You can place base struct instance inside nested and use alias
 this. Note that currently multiple alias this are not supported.
 Also note that you cannot override functions because there are no
 virtual table for structs.
why multiple alias this not supported? i know it was broken in 2.060 but with 2.061 it was fixed right?
It's simply never been implemented. -- Simen
May 12 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Sunday, May 12, 2013 14:00:49 evilrat wrote:
 why multiple alias this not supported? i know it was broken in
 2.060 but with 2.061 it was fixed right?
Multiple alias this-es have never worked. They're described in TDPL, but they've never been implemented. Hopefully, they'll get implemented at some point, but AFAIK, no one has been working on them yet, and no one knows when they might be implemented. - Jonathan M Davis
May 12 2013
prev sibling parent "Dicebot" <m.strashun gmail.com> writes:
I believe template mixin are the way to go for structs. Small 
annotated example:

// http://dpaste.1azy.net/56dd2513

mixin template Operators()
{
	// Verify "a" field exists for better compile-time error. 
Hard-coded here.
	// As an alternative, name of field to use can be provided as a 
template argument.
	static assert(is(typeof(typeof(this).init.a)),
		"class/struct that used this template mixin must have 'a' 
member declared");
	
	// Simple implementation for opBinary. As template mixins use 
scope of instatiator,
	// "this" will have target class/struct	type. Basically, you can 
write here any
	// implementation you would normally do in a base class.
	typeof(this) opBinary(string op)(typeof(this) rhs)
		if (op == "+")
	{
		return Test(this.a + rhs.a);
	}
}

struct Test
{
	int a;
	
	// that simple, almost same code amount as with "Test : TestBase"
	mixin Operators;
}

void main()
{
	Test x1 = { 2 };
	Test x2 = { 3 };
	import std.stdio;
	// prints "Test(5)"
	writeln(x1 + x2);
}
May 13 2013