Re: Structure's inheritance

2013-05-13 Thread Dicebot
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);
}


Structure's inheritance

2013-05-12 Thread Temtaime

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.


Re: Structure's inheritance

2013-05-12 Thread evilrat

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?


Re: Structure's inheritance

2013-05-12 Thread Namespace

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]


Re: Structure's inheritance

2013-05-12 Thread Maxim Fomin

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.


Re: Structure's inheritance

2013-05-12 Thread 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?


Re: Structure's inheritance

2013-05-12 Thread Dmitry Olshansky

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


Re: Structure's inheritance

2013-05-12 Thread Simen Kjaeraas

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


Re: Structure's inheritance

2013-05-12 Thread Jonathan M Davis
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