Re: Struct inheritance

2015-02-24 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Feb 24, 2015 at 06:22:05PM +, ketmar via Digitalmars-d-learn wrote:
> On Tue, 24 Feb 2015 12:05:50 +, amber wrote:
> 
> > Hi,
> > 
> > Is it possible in D to have inheritance using value types, i.e.
> > structs?
> > 
> > Also I don't quite understand how copy ctors work in D. Do I need to
> > implement opAssign(S other) {}, or this(this) {} and what's the
> > difference between these two?
> > 
> > Thanks,
> > Amber
> 
> p.s. sometimes compiler can use "move" instead of "copy" for
> structures.  in this case it will not call postblit. so if you have
> some fields in your struct that depends of the structure address...
> you're in trouble.

Yes, basically, D structs are value types, and quite unlike C++ structs.
It's a trap to assume C++-like semantics for D structs, because they are
actually very different beasts. D structs should not rely on their own
address remaining static, because move semantics will cause problems,
for example:

struct S {
int x;
int* p;
this(int _x) {
x = _x;
p = &x; // uh-oh
}
void method() {
*p++;   // by the time this runs, the struct may
// have moved
}
}

S makeStruct() {
return S(1);
}

void main() {
auto s = makeStruct();
s.method();
assert(s.x == 2); // KABOOM
}

The problem is that returning S from makeStruct() makes a bitwise copy
of the struct into main's local variable s, which has a different
address than the one created in makeStruct(). But s.p still points to
the old address of s.x, which is now pointing to invalid memory.

For even more fun, do this:

void checkItOut(int x, S* s) {
assert(x == 10);
s.method();
assert(x == 10); // KABOOM
}

void main() {
auto s = makeStruct();
s.method();
checkItOut(10, &s);
}

Depending on the specifics of your platform, the second assert in
checkItOut() may fail, because s.method(), via the stale pointer s.p,
will overwrite the stack location where the original s.x was (but which
has gone out of scope and is now invalid), and that old location is now
being used for one of the parameters of checkItOut(). So calling
s.method() will silently corrupt the stack where the parameter is
passed.

tl;dr, the moral of the story is, don't rely on the address of structs
in D staying the same, unless you REALLY know what you're doing. And
even then, you're just 1/8 of an inch away from shooting yourself in the
foot.


T

-- 
All problems are easy in retrospect.


Re: Struct inheritance

2015-02-24 Thread ketmar via Digitalmars-d-learn
On Tue, 24 Feb 2015 12:05:50 +, amber wrote:

> Hi,
> 
> Is it possible in D to have inheritance using value types, i.e. structs?
> 
> Also I don't quite understand how copy ctors work in D. Do I need to
> implement opAssign(S other) {}, or this(this) {} and what's the
> difference between these two?
> 
> Thanks,
> Amber

p.s. sometimes compiler can use "move" instead of "copy" for structures. 
in this case it will not call postblit. so if you have some fields in 
your struct that depends of the structure address... you're in trouble.

signature.asc
Description: PGP signature


Re: Struct inheritance

2015-02-24 Thread ketmar via Digitalmars-d-learn
On Tue, 24 Feb 2015 18:19:39 +, ketmar wrote:

> On Tue, 24 Feb 2015 12:05:50 +, amber wrote:
> 
>> Hi,
>> 
>> Is it possible in D to have inheritance using value types, i.e.
>> structs?
>> 
>> Also I don't quite understand how copy ctors work in D. Do I need to
>> implement opAssign(S other) {}, or this(this) {} and what's the
>> difference between these two?
> 
> there is no "copy constructor" in D. structs are copied by `memcpy()`,
> and then compiler calls "postblit" aka `this(this)`. it may seem like
> constructor, but it's not. it's a "fixup", that fixes inconsistencies
> that are left by `memcpy()`. i.e. when postblit is called, structure is
> already copied.
> 
> and `opAssign` is... well, overloading of "=" operator. it has to
> manually create a structure copy and manually copy everything you need.
> and it will never be called by compiler "behind the scenes", like
> postblit.

oops. of course, you already has a struct for `opAssign` -- it need 
`this` after all! ;-) but no automatic copying. it's just a function 
call, nothing magic here.

signature.asc
Description: PGP signature


Re: Struct inheritance

2015-02-24 Thread ketmar via Digitalmars-d-learn
On Tue, 24 Feb 2015 12:05:50 +, amber wrote:

> Hi,
> 
> Is it possible in D to have inheritance using value types, i.e. structs?
> 
> Also I don't quite understand how copy ctors work in D. Do I need to
> implement opAssign(S other) {}, or this(this) {} and what's the
> difference between these two?

there is no "copy constructor" in D. structs are copied by `memcpy()`, 
and then compiler calls "postblit" aka `this(this)`. it may seem like 
constructor, but it's not. it's a "fixup", that fixes inconsistencies 
that are left by `memcpy()`. i.e. when postblit is called, structure is 
already copied.

and `opAssign` is... well, overloading of "=" operator. it has to 
manually create a structure copy and manually copy everything you need. 
and it will never be called by compiler "behind the scenes", like 
postblit.

signature.asc
Description: PGP signature


Re: Struct inheritance

2015-02-24 Thread amber via Digitalmars-d-learn
On Tuesday, 24 February 2015 at 12:16:43 UTC, Tobias Pankrath 
wrote:

On Tuesday, 24 February 2015 at 12:05:51 UTC, amber wrote:

Hi,

Is it possible in D to have inheritance using value types, 
i.e. structs?


No runtime polymorphism, but a kind of sub typing via alias 
this.


struct S { void foo() { writeln("S.foo"); }
struct T { S s; alias s this; }

T t;
t.foo(); // prints S.foo


Also I don't quite understand how copy ctors work in D. Do I 
need to implement opAssign(S other) {}, or this(this) {} and 
what's the difference between these two?


If available, opAssign will be used in an assignment like x = y;
You're custom opAssign can take arbitrary parameter types, so 
typeof(y) does not have to be typeof(x).


postblit is used for copy construction. This could be 
assignment if no opAssign is provided (not sure about this), 
but also e.g. passing parameter by value or returning from a 
functions


Thank you Tobias I get it now.

/amber


Re: Struct inheritance

2015-02-24 Thread Tobias Pankrath via Digitalmars-d-learn

On Tuesday, 24 February 2015 at 12:05:51 UTC, amber wrote:

Hi,

Is it possible in D to have inheritance using value types, i.e. 
structs?


No runtime polymorphism, but a kind of sub typing via alias this.

struct S { void foo() { writeln("S.foo"); }
struct T { S s; alias s this; }

T t;
t.foo(); // prints S.foo


Also I don't quite understand how copy ctors work in D. Do I 
need to implement opAssign(S other) {}, or this(this) {} and 
what's the difference between these two?


If available, opAssign will be used in an assignment like x = y;
You're custom opAssign can take arbitrary parameter types, so 
typeof(y) does not have to be typeof(x).


postblit is used for copy construction. This could be assignment 
if no opAssign is provided (not sure about this), but also e.g. 
passing parameter by value or returning from a function.


Struct inheritance

2015-02-24 Thread amber via Digitalmars-d-learn

Hi,

Is it possible in D to have inheritance using value types, i.e. 
structs?


Also I don't quite understand how copy ctors work in D. Do I need 
to implement opAssign(S other) {}, or this(this) {} and what's 
the difference between these two?


Thanks,
Amber


Re: Struct "inheritance"

2012-02-05 Thread Daniel Murphy
I guess you've found a bug then. :)

"Vidar Wahlberg"  wrote in message 
news:jgm7sh$k4u$1...@digitalmars.com...
> On 2012-02-05 15:19, Daniel Murphy wrote:
>> The names only need to match if the compiler/build tool has to find the
>> module itself.  If you call the compiler with all modules listed:
>> gdc bar.d Foo.d etc.d
>> then it should be able to work it out.  (This is how it works with dmd,
>> anyway.  GDC is probably the same)
>
> Actually, that is what I do. GDC does not seem to figure it out:
> naushika:~/tmp> cat Foo.d
> module foo;
> ...
> naushika:~/tmp> gdc-4.6 bar.d Foo.d baz.d
> bar.d:2: Error: module foo is in file 'foo.d' which cannot be read
> import path[0] = /usr/include/d2/4.6/x86_64-linux-gnu
> import path[1] = /usr/include/d2/4.6
> naushika:~/tmp> mv Foo.d foo.d
> naushika:~/tmp> gdc-4.6 bar.d foo.d baz.d
> naushika:~/tmp> 




Re: Struct "inheritance"

2012-02-05 Thread Vidar Wahlberg

On 2012-02-05 15:19, Daniel Murphy wrote:

The names only need to match if the compiler/build tool has to find the
module itself.  If you call the compiler with all modules listed:
gdc bar.d Foo.d etc.d
then it should be able to work it out.  (This is how it works with dmd,
anyway.  GDC is probably the same)


Actually, that is what I do. GDC does not seem to figure it out:
naushika:~/tmp> cat Foo.d
module foo;
...
naushika:~/tmp> gdc-4.6 bar.d Foo.d baz.d
bar.d:2: Error: module foo is in file 'foo.d' which cannot be read
import path[0] = /usr/include/d2/4.6/x86_64-linux-gnu
import path[1] = /usr/include/d2/4.6
naushika:~/tmp> mv Foo.d foo.d
naushika:~/tmp> gdc-4.6 bar.d foo.d baz.d
naushika:~/tmp>


Re: Struct "inheritance"

2012-02-05 Thread Daniel Murphy
"Vidar Wahlberg"  wrote in message 
news:jgm2qk$c2g$1...@digitalmars.com...
> Adding a note about GDC (4.6.2) here:
> It appears like it ignores "module ;" and always use the filename 
> for module name (or I've misunderstood what "module" is used for). If I 
> create a file "Foo.d" which contains "module foo;", then in any other file 
> I wish to include module "foo" in I must write "include Foo;", not 
> "include foo;".

The names only need to match if the compiler/build tool has to find the 
module itself.  If you call the compiler with all modules listed:
gdc bar.d Foo.d etc.d
then it should be able to work it out.  (This is how it works with dmd, 
anyway.  GDC is probably the same) 




Re: Struct "inheritance"

2012-02-05 Thread Vidar Wahlberg

On 2012-02-05 14:16, Simen Kjærås wrote:

On Sun, 05 Feb 2012 11:58:40 +0100, Vidar Wahlberg 
wrote:

Also, is this really ambiguous? Are there any cases where you can have
a module name followed by a parentheses, i.e. "("?


Not that I know.


Possibly something that could make the language slightly more newbie 
friendly here, then. For now I'll just keep the filenames in lowercase 
so i "import struct;" rather than "import Struct;" (I see the norm for D 
is to keep the filenames in lowercase, might as well follow it).


Adding a note about GDC (4.6.2) here:
It appears like it ignores "module ;" and always use the filename 
for module name (or I've misunderstood what "module" is used for). If I 
create a file "Foo.d" which contains "module foo;", then in any other 
file I wish to include module "foo" in I must write "include Foo;", not 
"include foo;".




I'm using gdc-4.6 (Debian 4.6.2-4).


Ah. That's the equivalent of DMD 2.054. I don't have that installed, but
it may be that this feature was not added until after that.

Fix: install GDC 4.6.1: https://bitbucket.org/goshawk/gdc/downloads


I'm running GDC 4.6.2, not 4.6.0 (I just pasted output from "gdc-4.6 
--version", I could've made it clearer), downgrading probably won't help me.




Workaround: Use a templated opEquals:

struct Struct {
int baz;
bool opEquals()(const Struct s) const {
return baz == s.baz;
}
}

Hope this works.


Yes, it does. I have to read a bit about templating as I don't 
understand exactly what that "()" means, but it did solve my problem, 
thanks!


Re: Struct "inheritance"

2012-02-05 Thread Simen Kjærås
On Sun, 05 Feb 2012 11:58:40 +0100, Vidar Wahlberg   
wrote:


Also, is this really ambiguous? Are there any cases where you can have a  
module name followed by a parentheses, i.e. "("?


Not that I know.



 > I cannot seem to recreate this error message. Which version of the
 > compiler are you using?

I'm using gdc-4.6 (Debian 4.6.2-4).
Using the Struct from above I can easily recreate the error:
struct Struct {
   int baz;
   bool opEquals(const Struct s) const {
 return baz == s.baz;
   }
}


Ah. That's the equivalent of DMD 2.054. I don't have that installed, but
it may be that this feature was not added until after that.

Fix: install GDC 4.6.1: https://bitbucket.org/goshawk/gdc/downloads

Workaround: Use a templated opEquals:

struct Struct {
int baz;
bool opEquals()(const Struct s) const {
return baz == s.baz;
}
}

Hope this works.


Re: Struct "inheritance"

2012-02-05 Thread Vidar Wahlberg
Sending this again, got an error the first time and it appears like it 
was not sent.


On 2012-02-04 21:48, Simen Kjærås wrote:
> I see. There's a hint in the error message: "function expected [...],
> not module". Struct is the name of a module, so the compiler thinks
> you want to access something inside it. If you want 'Struct' to refer
> to the type, you must use selective import[1]:
>
> import Struct : Struct;

I see, thanks, that does solve the problem. It is slightly confusing 
that you don't get this kind of error for "Foo" in "Bar.d", though 
(compiler seems to understand that I mean the class "Foo" rather than 
the module "Foo" in this code: "_foo = new Foo(Struct(1))").


Also, is this really ambiguous? Are there any cases where you can have a 
module name followed by a parentheses, i.e. "("?



> I cannot seem to recreate this error message. Which version of the
> compiler are you using?

I'm using gdc-4.6 (Debian 4.6.2-4).
Using the Struct from above I can easily recreate the error:
struct Struct {
  int baz;
  bool opEquals(const Struct s) const {
return baz == s.baz;
  }
}


Re: Struct "inheritance"

2012-02-04 Thread Ali Çehreli

On 02/04/2012 08:25 AM, Ali Çehreli wrote:

The following templatizes the coordinate types, but you could use put
write everywhere:


That should be "... you could write *ints* everywhere".

Ali


Re: Struct "inheritance"

2012-02-04 Thread Ali Çehreli

On 02/04/2012 03:38 AM, Vidar Wahlberg wrote:

> Let's say I got a struct for a location on a 2-dimensional plane:
> struct Point {
> int x;
> int y;
> }
> Further I also need to represent a location in a 3-dimensional space:
> struct Coordinate {
> int x;
> int y;
> int z;
> }
> If these were classes instead I could simply make Coordinate inherit
> from Point and only add "int z;".

There is also template mixins to inject complete features into the code 
(similar to C macros but without their gotchas).


The following templatizes the coordinate types, but you could use put 
write everywhere:


import std.stdio;

template Point2D(T)
{
T x;
T y;

void foo2D()
{
writefln("Using (%s,%s)", x, y);
}
}

struct Coordinate(T)
{
mixin Point2D!T;   // <-- Inject x, y, and foo2D() here
T z;

this(T x, T y, T z)
{
this.x = x;
this.y = y;
this.z = z;
}
}

void main()
{
auto c = Coordinate!double(1.1, 2.2, 3.3);
c.foo2D();
}

You could insert the following line in any other scope and you would 
have x, y, foo2D() inserted right there as well:


if (someCondition) {
mixin Point2D!T;   // <-- Inject x, y, and foo2D() here

// ... use the local x, y, and foo2D() here
}

Of course 'static if' may be even more suitable in other cases.

Ali



Re: Struct "inheritance"

2012-02-04 Thread Vidar Wahlberg

On 2012-02-04 14:45, Simen Kjærås wrote:

Like bearophile said, Point(x, y) should work - assuming you have
defined no other constructors for Point.


You are correct, my apologies for not testing more thoroughly.
Let me try again, explaining the real issue:
I have 3 files:
-- Bar.d --
import Foo;
import Struct;
class Bar {
  Foo _foo;
  this() {
_foo = new Foo(Struct(1));
  }
}
void main() {
  new Bar();
}

-- Foo.d --
import Struct;
class Foo {
  Struct _s;
  this(Struct s) {
_s = s;
  }
}

-- Struct.d --
struct Struct {
  int baz;
}

This code does not compile:
Bar.d:6: Error: function expected before (), not module Struct of type void
Bar.d:6: Error: constructor Foo.Foo.this (Struct s) is not callable 
using argument types (_error_)


Why is that?



It is. The problem is that bool opEquals(ref const Point) expects a Point
by reference, and the return value from your property is a temporary, from
which we can get no reference. The solution is to remove the ref:
bool opEquals(const Point). The only reason to use ref here is to cut down
on copying, and might be worthwhile on larger structures.


I've tried removing the "ref", but I get other errors in return then:
Point.d:19: Error: function Point.Point.opEquals type signature should 
be const bool(ref const(Point)) not const bool(const const(Point) p)
Coordinate.d:20: Error: function Coordinate.Coordinate.opEquals type 
signature should be const bool(ref const(Coordinate)) not const 
bool(const const(Coordinate) c)


Re: Struct "inheritance"

2012-02-04 Thread Simen Kjærås
On Sat, 04 Feb 2012 13:55:55 +0100, Vidar Wahlberg   
wrote:



On 2012-02-04 13:06, Simen Kjærås wrote:

It seems that what you want is alias this:


Thank you both, that's exactly what I needed.

Leeching a bit more on the thread:
Going back to the method:
int somethingNifty(Point p) {
   return p.x + p.y;
}

Let's say I have the following code:
for (x; 0 .. 10) {
   for (y; 0 .. 10) {
 Point p = {x, y};
 somethingNifty(p);
   }
}

[How] can you rewrite those two statements inside the loops to a single  
line? For example (this doesn't work):

somethingNifty(Point(x, y));


Like bearophile said, Point(x, y) should work - assuming you have
defined no other constructors for Point.

You can also explicitly define a constructor that does what you want.


And finally a question about operator overloading, here's the code for  
"Coordinate" ("Point" is similar in structure):

import Point;
struct Coordinate {
   Point _point;
   int _z;
   @property auto point() const {
 return _point;
   }
   @property auto point(Point point) {
 return _point = point;
   }
   @property auto z() const {
 return _z;
   }
   @property auto z(int z) {
 return _z = z;
   }
   bool opEquals(ref const Coordinate c) const {
 return z == c.z && point == c.point;
   }
}

Compilation fails with the following error:
Coordinate.d:18: Error: function Point.Point.opEquals (ref const  
const(Point) p) const is not callable using argument types  
(const(Point)) const

Coordinate.d:18: Error: c.point() is not an lvalue

Noteworthy the code compiles fine if i replace "point" and "c.point"  
with "_point" and "c._point", but then I'm referencing _point directly  
instead of through the property "point" (which may do something else  
than just return _point in the future).
I've looked at  
http://www.d-programming-language.org/operatoroverloading.html#equals  
and that page is slightly confusing. It claims that:
If structs declare an opEquals member function, it should follow the  
following form:

struct S {
   int opEquals(ref const S s) { ... }
}

However, I can't even get the code to compile if I do that, the compiler  
(gdc-4.6) says:
Error: function Coordinate.Coordinate.opEquals type signature should be  
const bool(ref const(Coordinate)) not int(ref const const(Coordinate) c)



I hope it's somewhat clear what I'm trying to achieve.


It is. The problem is that bool opEquals(ref const Point) expects a Point
by reference, and the return value from your property is a temporary, from
which we can get no reference. The solution is to remove the ref:
bool opEquals(const Point). The only reason to use ref here is to cut down
on copying, and might be worthwhile on larger structures.


Re: Struct "inheritance"

2012-02-04 Thread bearophile
Vidar Wahlberg:

> Leeching a bit more on the thread:
> Going back to the method:
> int somethingNifty(Point p) {
>return p.x + p.y;
> }
> 
> Let's say I have the following code:
> for (x; 0 .. 10) {
>for (y; 0 .. 10) {
>  Point p = {x, y};
>  somethingNifty(p);
>}
> }
> 
> [How] can you rewrite those two statements inside the loops to a single 
> line? For example (this doesn't work):
> somethingNifty(Point(x, y));

This works:

struct Point {
int x, y;
}

int somethingNifty(Point p) {
return p.x + p.y;
}

void main( ) {
foreach (x; 0 .. 10)
foreach (y; 0 .. 10)
somethingNifty(Point(x, y));
}

Bye,
bearophile


Re: Struct "inheritance"

2012-02-04 Thread Vidar Wahlberg

On 2012-02-04 13:06, Simen Kjærås wrote:

It seems that what you want is alias this:


Thank you both, that's exactly what I needed.

Leeching a bit more on the thread:
Going back to the method:
int somethingNifty(Point p) {
  return p.x + p.y;
}

Let's say I have the following code:
for (x; 0 .. 10) {
  for (y; 0 .. 10) {
Point p = {x, y};
somethingNifty(p);
  }
}

[How] can you rewrite those two statements inside the loops to a single 
line? For example (this doesn't work):

somethingNifty(Point(x, y));


And finally a question about operator overloading, here's the code for 
"Coordinate" ("Point" is similar in structure):

import Point;
struct Coordinate {
  Point _point;
  int _z;
  @property auto point() const {
return _point;
  }
  @property auto point(Point point) {
return _point = point;
  }
  @property auto z() const {
return _z;
  }
  @property auto z(int z) {
return _z = z;
  }
  bool opEquals(ref const Coordinate c) const {
return z == c.z && point == c.point;
  }
}

Compilation fails with the following error:
Coordinate.d:18: Error: function Point.Point.opEquals (ref const 
const(Point) p) const is not callable using argument types 
(const(Point)) const

Coordinate.d:18: Error: c.point() is not an lvalue

Noteworthy the code compiles fine if i replace "point" and "c.point" 
with "_point" and "c._point", but then I'm referencing _point directly 
instead of through the property "point" (which may do something else 
than just return _point in the future).
I've looked at 
http://www.d-programming-language.org/operatoroverloading.html#equals 
and that page is slightly confusing. It claims that:
If structs declare an opEquals member function, it should follow the 
following form:

struct S {
  int opEquals(ref const S s) { ... }
}

However, I can't even get the code to compile if I do that, the compiler 
(gdc-4.6) says:
Error: function Coordinate.Coordinate.opEquals type signature should be 
const bool(ref const(Coordinate)) not int(ref const const(Coordinate) c)



I hope it's somewhat clear what I'm trying to achieve.


Re: Struct "inheritance"

2012-02-04 Thread Simen Kjærås
On Sat, 04 Feb 2012 12:38:30 +0100, Vidar Wahlberg   
wrote:



Good day.

I know "inheritance" is a misleading word as there's no such thing when  
it comes to structs, but I couldn't think of a better description for  
this problem:


Let's say I got a struct for a location on a 2-dimensional plane:
struct Point {
   int x;
   int y;
}
Further I also need to represent a location in a 3-dimensional space:
struct Coordinate {
   int x;
   int y;
   int z;
}
If these were classes instead I could simply make Coordinate inherit  
from Point and only add "int z;". This would also make the thing I'm  
trying to achieve much easier; Consider I have a method that does  
something nifty with "x" and "y", I'd like this method to handle both  
"Point" and "Coordinate", if they were classes it would be fairly simple:

int somethingNifty(Point p) {
   return p.x + p.y;
}


So why not just use classes? I've understood it as there may be a  
performance gain by using structs over classes, and in my program Point  
and Coordinate are used heavily.


Obviously I'm fairly new to both D and structs, I have plenty Java and  
some C++ experience (I've avoided using structs in C++) and I have "The  
D Programming Language" book by Andrei which I'm happy to look up in,  
although I've failed to find an answer to this question in the book, in  
this newsgroup and on the net.
I would greatly appreciate if someone could give me a nudge the the  
right direction.


It seems that what you want is alias this:

struct Point {
int x;
int y;
}

struct Coordinate {
Point pt;
int z;
alias pt this;
}

void foo( Point p ) {}

void main( ) {
Coordinate c;
foo( c );
c.x = 3;
c.y = 4;
c.z = 5;
}


Struct "inheritance"

2012-02-04 Thread Vidar Wahlberg

Good day.

I know "inheritance" is a misleading word as there's no such thing when 
it comes to structs, but I couldn't think of a better description for 
this problem:


Let's say I got a struct for a location on a 2-dimensional plane:
struct Point {
  int x;
  int y;
}
Further I also need to represent a location in a 3-dimensional space:
struct Coordinate {
  int x;
  int y;
  int z;
}
If these were classes instead I could simply make Coordinate inherit 
from Point and only add "int z;". This would also make the thing I'm 
trying to achieve much easier; Consider I have a method that does 
something nifty with "x" and "y", I'd like this method to handle both 
"Point" and "Coordinate", if they were classes it would be fairly simple:

int somethingNifty(Point p) {
  return p.x + p.y;
}


So why not just use classes? I've understood it as there may be a 
performance gain by using structs over classes, and in my program Point 
and Coordinate are used heavily.


Obviously I'm fairly new to both D and structs, I have plenty Java and 
some C++ experience (I've avoided using structs in C++) and I have "The 
D Programming Language" book by Andrei which I'm happy to look up in, 
although I've failed to find an answer to this question in the book, in 
this newsgroup and on the net.
I would greatly appreciate if someone could give me a nudge the the 
right direction.


Re: Struct "inheritance"

2012-02-04 Thread Trass3r
So why not just use classes? I've understood it as there may be a  
performance gain by using structs over classes, and in my program Point  
and Coordinate are used heavily.


The other big difference is value vs. reference type.
You can use alias this to achieve something like "struct inheritance".


Re: Struct "inheritance"

2012-02-04 Thread Simen Kjærås
On Sat, 04 Feb 2012 15:04:50 +0100, Vidar Wahlberg   
wrote:



This code does not compile:
Bar.d:6: Error: function expected before (), not module Struct of type  
void
Bar.d:6: Error: constructor Foo.Foo.this (Struct s) is not callable  
using argument types (_error_)


Why is that?


I see. There's a hint in the error message: "function expected [...],
not module". Struct is the name of a module, so the compiler thinks
you want to access something inside it. If you want 'Struct' to refer
to the type, you must use selective import[1]:

import Struct : Struct;

This says 'I want only the type Struct from the module Struct, not
everything else in there.'

The other solution is to simply use different names for the type and
the module.


It is. The problem is that bool opEquals(ref const Point) expects a  
Point
by reference, and the return value from your property is a temporary,  
from

which we can get no reference. The solution is to remove the ref:
bool opEquals(const Point). The only reason to use ref here is to cut  
down

on copying, and might be worthwhile on larger structures.


I've tried removing the "ref", but I get other errors in return then:
Point.d:19: Error: function Point.Point.opEquals type signature should  
be const bool(ref const(Point)) not const bool(const const(Point) p)
Coordinate.d:20: Error: function Coordinate.Coordinate.opEquals type  
signature should be const bool(ref const(Coordinate)) not const  
bool(const const(Coordinate) c)


I cannot seem to recreate this error message. Which version of the
compiler are you using?

[1]: http://www.d-programming-language.org/module.html#ImportDeclaration


Re: struct inheritance need?

2008-12-25 Thread Weed
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.


Re: struct inheritance need?

2008-12-25 Thread Weed

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.


Re: struct inheritance need?

2008-12-25 Thread 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.


Re: struct inheritance need?

2008-12-24 Thread Weed

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.


Re: struct inheritance need?

2008-12-24 Thread 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.


Re: struct inheritance need?

2008-12-24 Thread Weed

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/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.


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 :)


Re: struct inheritance need?

2008-12-24 Thread Weed

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:)


Re: struct inheritance need?

2008-12-24 Thread 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.


Re: struct inheritance need?

2008-12-23 Thread Weed

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


Re: struct inheritance need?

2008-12-23 Thread 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?


Re: struct inheritance need?

2008-12-23 Thread 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/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.


Re: struct inheritance need?

2008-12-23 Thread 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.

I see no way, how it can help.


Re: struct inheritance need?

2008-12-23 Thread 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.


Re: struct inheritance need?

2008-12-22 Thread Weed

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


Re: struct inheritance need?

2008-12-22 Thread 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. I'd prefer run-time checks, though templates can be used for sure.


Re: struct inheritance need?

2008-12-22 Thread Sergey Gromov
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.


Compile-time reference type objects construction (Was - Re: struct inheritance need?)

2008-12-21 Thread Denis Koroskin

On Sun, 21 Dec 2008 19:34:52 +0300, Kagamin  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).


Re: struct inheritance need?

2008-12-21 Thread Weed

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=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.


...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".


Re: struct inheritance need?

2008-12-21 Thread 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=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.


Re: struct inheritance need?

2008-12-21 Thread 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.


Re: struct inheritance need?

2008-12-21 Thread Kagamin
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 :)


Re: struct inheritance need?

2008-12-21 Thread Weed

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


Re: struct inheritance need?

2008-12-21 Thread Weed

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 even this edition: 
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945


Re: struct inheritance need?

2008-12-21 Thread 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.


Re: struct inheritance need?

2008-12-20 Thread Christopher Wright

Denis Koroskin wrote:
On Sat, 20 Dec 2008 16:46:44 +0300, Christopher Wright 
 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).


Re: struct inheritance need?

2008-12-20 Thread Denis Koroskin
On Sat, 20 Dec 2008 16:46:44 +0300, Christopher Wright  
 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; }
}


Re: struct inheritance need?

2008-12-20 Thread Christopher Wright

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?


Re: struct inheritance need?

2008-12-19 Thread Derek Parnell
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


Re: struct inheritance need?

2008-12-19 Thread Sergey Gromov
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]]);


Re: struct inheritance need?

2008-12-19 Thread Sergey Gromov
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.


Re: struct inheritance need?

2008-12-19 Thread Kagamin
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.


Re: struct inheritance need?

2008-12-18 Thread Weed

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


Re: struct inheritance need?

2008-12-18 Thread Christopher Wright

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?


Re: struct inheritance need?

2008-12-18 Thread 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.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


Re: struct inheritance need?

2008-12-18 Thread Kagamin
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.


Re: struct inheritance need?

2008-12-18 Thread Kagamin
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);
}


Re: struct inheritance need?

2008-12-18 Thread naryl
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.


Re: struct inheritance need?

2008-12-18 Thread Weed

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);
}
---


Re: struct inheritance need?

2008-12-18 Thread 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.
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);
}
---


Re: struct inheritance need?

2008-12-18 Thread Weed

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:)



Re: struct inheritance need?

2008-12-18 Thread 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.


Re: struct inheritance need?

2008-12-18 Thread Kagamin
Well, the wish to place an object at specific location is not a problem, it's a 
wish.


Re: struct inheritance need?

2008-12-18 Thread 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.


Re: struct inheritance need?

2008-12-18 Thread Weed

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?


Re: struct inheritance need?

2008-12-18 Thread 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? Static 
variables are initialized with static constructors. Global variables are static 
members of module, wich is similar to class and also has static constructor.


Re: struct inheritance need?

2008-12-17 Thread Weed

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


Re: struct inheritance need?

2008-12-17 Thread 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.






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


Re: struct inheritance need?

2008-12-17 Thread Weed

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


Re: struct inheritance need?

2008-12-17 Thread 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


Re: struct inheritance need?

2008-12-17 Thread 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 creating an object of class or (most likely wrong) struct 
inheritance.


I have prepared a distinct feature request, send it later


Re: struct inheritance need?

2008-12-17 Thread 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?


Re: struct inheritance need?

2008-12-17 Thread Weed

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


Re: struct inheritance need?

2008-12-17 Thread 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)(...)
{

}


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


Re: struct inheritance need?

2008-12-17 Thread Weed

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?


Re: struct inheritance need?

2008-12-17 Thread 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.


Re: struct inheritance need?

2008-12-17 Thread 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.


Re: struct inheritance need?

2008-12-16 Thread Weed

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?


Re: struct inheritance need?

2008-12-16 Thread Yigal Chripun

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".

I think not worth taking structs and classes from C#. May be bytecode
interpreter C# does not show the difference in speed between the
allocation of memory by a small object + its using and the use of a
static object, so developers C# decided to do so as done. (but I am not
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?)


Re: struct inheritance need?

2008-12-16 Thread Weed

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".


I think not worth taking structs and classes from C#. May be bytecode 
interpreter C# does not show the difference in speed between the 
allocation of memory by a small object + its using and the use of a 
static object, so developers C# decided to do so as done. (but I am not 
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.


Re: struct inheritance need?

2008-12-16 Thread 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 :-)

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


Re: struct inheritance need?

2008-12-16 Thread Weed

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?


Re: struct inheritance need?

2008-12-16 Thread Weed

Bill Baxter пишет:

2008/12/16 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



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.


Re: struct inheritance need?

2008-12-16 Thread Weed

Bill Baxter пишет:

2008/12/16 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



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 common parent.


Re: struct inheritance need?

2008-12-16 Thread Bill Baxter
2008/12/16 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
>

Here's one way:
http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d

--bb


struct inheritance need?

2008-12-16 Thread 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