Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Steven Schveighoffer
On Thu, 27 May 2010 18:41:19 -0400, bearophile wrote: Thank you Steven for your explanations, I have done similar things in C and D, but I didn't understand what you meant. A is always put first, that way, a pointer to a B can always be used as a pointer to an A.< Are you sure C specs

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Thank you Steven for your explanations, I have done similar things in C and D, but I didn't understand what you meant. >A is always put first, that way, a pointer to a B can always be used as a >pointer to an A.< Are you sure C specs say doing this leads to defined behaviour? (Recently from a d

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Ali: > For what it's worth, here is a code that uses 'dup' instead of 'copy'. Oh, right. I have forgotten to say this to the OP. In D it's better to name it dup instead of copy. >I know this is not the same thing; but I find this more intuitive:< I agree, it's better for D. > A dup() co

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Steven Schveighoffer
On Thu, 27 May 2010 17:47:20 -0400, bearophile wrote: Steven Schveighoffer: I have hoped that at some point, structs can be auto-composed, without a vtable, but you still have to do this manually. I don't understand what you mean here :-) I mean simple inheritance. In C, there has alway

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Ali Çehreli
bearophile wrote: Larry Luther: Ok, I've added -w to compilation commands and I've switched back to pure text.< Good :-) What am I missing?< I have modified a bit your D code like this, to have something with a main() that runs: import std.c.stdio: puts; class A { int x, y;

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Steven Schveighoffer: > I have hoped that at some point, structs can be auto-composed, > without a vtable, but you still have to do this manually. I don't understand what you mean here :-) Bye, bearophile

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Steven Schveighoffer
On Thu, 27 May 2010 17:04:35 -0400, Larry Luther wrote: "bearophile" wrote in message news:ht4krg$17l...@digitalmars.com... | On the base of your long experience do you like D so far? There are many things that I like and I strongly agree with the failings of C++ mentioned in the docs. I d

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Larry Luther: > There are many things that I like and I strongly agree with the failings > of C++ mentioned in the docs. D is designed by people that have a good experience of C++, but while probably D avoids some C++ problems, it surely introduces a number of new issues :-) > I don't like the

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Larry Luther
"bearophile" wrote in message news:ht4krg$17l...@digitalmars.com... | On the base of your long experience do you like D so far? There are many things that I like and I strongly agree with the failings of C++ mentioned in the docs. I don't like the asymmetry between structs and classes. I don't

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
See: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=110554

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Larry Luther: >Ok, I've added -w to compilation commands and I've switched back to pure text.< Good :-) >What am I missing?< I have modified a bit your D code like this, to have something with a main() that runs: import std.c.stdio: puts; class A { int x, y; void copy(const A a) {

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread bearophile
Larry Luther: I'm nonplussed. Could you expand on why D class instances don't need to copy their contents and instances of D structs do? While migrating C++ code to D I've had to convert "struct"s to "class"es because of the need for inheritance. Why would the need to copy an instance's conte

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Larry Luther
"bearophile" wrote in message news:ht4g3r$vu...@digitalmars.com... | Larry Luther: | | > I did not get an error when building and running with DMD 2.042: | | I am using dmd v2.046, and I have taken the good habit of compiling with -w (warnings on). | It seems this error I see is a blocking warni

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Larry Luther
"bearophile" wrote in message news:ht42el$6c...@digitalmars.com... ... | There is no standard way to do this. In general you don't need to copy classes or their contents, because they are on the heap and managed by the GC. I'm nonplussed. Could you expand on why D class instances don't nee

Re: Newbie: copy, assignment of class instances

2010-05-27 Thread Larry Luther
Thank you, I had no idea that scope was doing this. I thought that when the docs said that it was being allocated on the stack that I was getting "struct" like behavior. "Simen kjaeraas" wrote in message news:op.vc0qlpjovxi...@biotronic-pc.home... | Larry Luther wrote: | | > | > scope B | >

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Larry Luther: > I'm a newbie to D, not to programming. The first computer I got to > program was a Digital PDP-8L. Wow :-) On the base of your long experience do you like D so far? > I'm learning D. And I am listing what I think can be improved in your D code :-) You are free to not fol

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Larry Luther
Ok, Ok, I'm a newbie to D, not to programming. The first computer I got to program was a Digital PDP-8L. I'm learning D. I'm learning how to use SlickEdit as an IDE for D. I've never used it to compile, debug, and execute before. Therefore it will take a while before I can figure

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Another bug report: http://d.puremagic.com/issues/show_bug.cgi?id=4216

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Ellery Newcomer: > Says the python nazi I admit that I am more indentation-nazi after using Python for few years, but I was strict on indentation even before hearing of Python :-) (In past I used 2 spaces, for older monitors). I have not written that html page was written by Walter before see

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Ellery Newcomer
On 05/20/2010 06:23 PM, bearophile wrote: You are a newbie, so let me point the little flaws in your D code, this is the D style guide: http://www.digitalmars.com/d/2.0/dstyle.html Regarding white space it says: * Use spaces instead of hardware tabs. * Each indentation level will be f

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Simen kjaeraas
bearophile wrote: Generally in D it's not a good idea to reassign the reference to a scoped class You can see it with this little D2 program: import std.stdio: printf; class Foo { int x; this(int xx) { this.x = xx; } ~this() { printf("Foo(%d) destructor\n", this.x); } } void ma

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
You are a newbie, so let me point the little flaws in your D code, this is the D style guide: http://www.digitalmars.com/d/2.0/dstyle.html Regarding white space it says: * Use spaces instead of hardware tabs. * Each indentation level will be four columns. Bye, bearophile

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Larry Luther: > I did not get an error when building and running with DMD 2.042: I am using dmd v2.046, and I have taken the good habit of compiling with -w (warnings on). It seems this error I see is a blocking warning. It's a warning badly written, but do you agree it is saying something impo

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Larry Luther: > void copy (in A a) { In D2 it's better to use "const" or "immutable" instead of "in". Here's one version of the code const-aware: import std.stdio: writeln; import std.string: format; template ExceptionTemplate() { this() { super(this.classinfo.name); }

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Larry Luther
I did not get an error when building and running with DMD 2.042: import std.stdio; class A { int x, y; void copy (in A a) { x = a.x; y = a.y; } void dump (string s) { writefln( "%s.A = { %s, %s }", s, x, y); } } class B : A { int z; void copy (in B b) { super.co

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Larry Luther: > B foo = new B, > B bar = new B; This is not valid D code, you have to use something like: B foo = new B; B bar = new B; I suggest you to actually try to compile and run your little test programs. If you compile this: class A { int x, y; void copy(A a) { x

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Larry Luther
Thank you for the clarifications. I'm trying to do a shallow copy. After reading Simen & bearophile, I would add a "copy" member function: class A { int x, y; void copy (in A a) { x = a.x; y = a.y; } } class B : A { int z; void copy (in B b) { super.

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
http://d.puremagic.com/issues/show_bug.cgi?id=4214

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
> Generally in D it's not a good idea to reassign the reference to a scoped > class You can see it with this little D2 program: import std.stdio: printf; class Foo { int x; this(int xx) { this.x = xx; } ~this() { printf("Foo(%d) destructor\n", this.x); } } void main() { scope F

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread bearophile
Larry Luther: > class A { > int x, y; > } > > class B : A { > int z; > } > > B > foo = new B, > bar = new B; > > scope B > alpha = new B; Don't write code like this, it's not easy to write: > B > foo = new B, > bar = new B; Write it this way: B foo

Re: Newbie: copy, assignment of class instances

2010-05-20 Thread Simen kjaeraas
Larry Luther wrote: Given: class A { int x, y; } class B : A { int z; } B foo = new B, bar = new B; scope B alpha = new B; Q1: How do I copy the member variables contributed by base class A from "foo" to "bar"? In C++: (A &) bar = foo; You can'

Newbie: copy, assignment of class instances

2010-05-20 Thread Larry Luther
Given: class A { int x, y; } class B : A { int z; } B foo = new B, bar = new B; scope B alpha = new B; Q1: How do I copy the member variables contributed by base class A from "foo" to "bar"? In C++: (A &) bar = foo; Q2: How do I do a deepcopy of fo