May 20, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Larry Luther | 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 |
May 20, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 four columns.
>
> Bye,
> bearophile
Says the python nazi <g>
|
May 20, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile <bearophileHUGS@lycos.com> 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 main() { > scope Foo f1 = new Foo(1); > Foo f2 = new Foo(2); > f1 = f2; > } > > It prints just: > Foo(2) destructor > > In general this is not good. > Bye, > bearophile It is indeed not. However, there are two bugs here - first, the wrong instance's destructor is called. Second, f1's destructor is never called. Surely this has roots in the same code, but they're two separate issues. Basically, with the current system, it must not be the reference's task to destroy an instance at end of scope, but the instance itself. The simplest way to fix this is, as you say, to disallow reassignment of a scope class reference. -- Simen |
May 20, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | Ellery Newcomer:
> Says the python nazi <g>
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 seeing me in the D newsgroups :-)
Having a well chosen and uniform style in a language is useful, it helps readability between different programmers of the D community of programmers.
You can also take a look at the quite strict C++ style guide (they forbid tabs, but use 2 spaces).
4 spaces is better than 2 in D today because monitors are wider, and because too many indentations in the code (more than five or six) is probably a sign of badly written code (here I am talking about production code, not toy programs).
Bye,
bearophile
|
May 20, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Another bug report: http://d.puremagic.com/issues/show_bug.cgi?id=4216 |
May 21, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 out where to add -w (Yes I like to turn on all warnings too). I'm relearning OutlookExpress for reading news. I wanted a fixed width font and couldn't configure OutlookExpress intuitively. The IT guy said use HTML and that worked, but you don't like it, so I've gotten heavy handed with OutlookExpress and it looks like I have it. I don't have any option for using spaces for indentation, (my preference too). The OutlookExpress editor must be replacing my spaces with tabs. The Association of Computing Machinery did a study of indentation a long time ago and found that indentations of 2 or 3 were best. I picked 2. "bearophile" <bearophileHUGS@lycos.com> wrote in message news:ht4ga0$1099$1@digitalmars.com... | 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 |
May 21, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Larry Luther | 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 follow my advice... > The IT guy said use HTML > and that worked, but you don't like it, It's the Web interface of the D groups that doesn't seem to like HTML :-) >The OutlookExpress editor must be replacing my spaces with tabs. I have seen 2 spaces in your code. > The Association of Computing Machinery did a study of indentation a long > time ago > and found that indentations of 2 or 3 were best. I picked 2. I have shown you the D style guide written by the Walter, the D main designer. Some people don't follow that style guide, but I like it and I think a language-community-wide style guide is important for a modern language. I too used to use 2 spaces religiously, but today monitors have 120+ columns, not 80 as (probably) when that ACM study was carried out. Things change as time goes on. If you use 2 columns it's a bit harder to see indentations and you can be more tempted to use many indentation levels in your code. Keeping four is a way to discourage the usage of too many indentation levels. In the end you are free to use the number of indentations you like in D snippets :-) Don't get upset, we are all learning :-) There are many things in D that I have yet to understand. Bye, bearophile |
May 27, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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" <simen.kjaras@gmail.com> wrote in message news:op.vc0qlpjovxi10f@biotronic-pc.home... | Larry Luther <larry.luther@dolby.com> wrote: | | > | > scope B | > alpha = new B; | > | | 'scope B alpha;' allocates stack space for a reference to a B, and | space for an instance of B. If the pointer changes, it is simply no longer | referencing the instance on the stack. | | -- | Simen |
May 27, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | "bearophile" <bearophileHUGS@lycos.com> wrote in message news:ht42el$6c9$1@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 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 contents to another instance disappear? Larry |
May 27, 2010 Re: Newbie: copy, assignment of class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | "bearophile" <bearophileHUGS@lycos.com> wrote in message news:ht4g3r$vue$1@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 warning. It's a warning badly written, but do you agree it is saying something important? I presume your code can lead to bugs. I don't know why this is a warning instead of a true error... | | On your code it also complains for a missed "override" that is a good thing that eventually will become obligatory even without -w. | | When you post here I suggest you to avoid using HTML and use pure text. | | Bye, | bearophile Ok, I've added -w to compilation commands and I've switched back to pure text. Given: class A { int x, y; void copy (const A a) { x = a.x; y = a.y; } } class B : A { int z; void copy (const B b) { super.copy( b); z = b.z; } } A alpha = new A; A bravo = new A; B charlie = new B; B delta = new B; ------------------------------- I don't see the problem with A.copy vs B.copy. alpha.copy( bravo) -- should execute A.copy alpha.copy( charlie) -- should execute A.copy charlie.copy( delta) -- should execute B.copy charlie.copy( alpha) -- should execute A.copy What am I missing? Larry |
Copyright © 1999-2021 by the D Language Foundation