Re: Confused about const

2010-03-20 Thread Paul D. Anderson
bearophile Wrote: > Paul D. Anderson: > > > After further review, I now realize that the right way (for me) to do this > > is to add a .dup property.< > > Steven Schveighoffer has given you quite good answers. > A dup is generally not enough, because what you dup can have other immutable > ref

Re: Confused about const

2010-03-20 Thread Pelle MÃ¥nsson
On 03/20/2010 01:58 AM, Paul D. Anderson wrote: I created a struct, call it "S", and some functions that operate on S. But I'm confused about when const is useful. Being an old Java programmer, I use 'const' the same as I used 'final' in Java. So most of my functions look like this: S for(con

Re: Confused about const

2010-03-20 Thread bearophile
Don: > I think the problem is really that it's still very buggy. In particular, > opAssign seems pretty defective. More importantly, struct constructors, > post blit, and struct destructors are still quite badly broken. You are right, but in my opinion it's not just a matter of bugs. To solve re

Re: Confused about const

2010-03-20 Thread Don
bearophile wrote: Operator overloading in D2 is not an easy thing, it needs training. >(That's why I have recently asked for the compiler to be strict to avoid wrong usages of the operator overloading.) I think the problem is really that it's still very buggy. In particular, opAssign seems pr

Re: Confused about const

2010-03-20 Thread bearophile
Paul D. Anderson: > After further review, I now realize that the right way (for me) to do this is > to add a .dup property.< Steven Schveighoffer has given you quite good answers. A dup is generally not enough, because what you dup can have other immutable references nested inside. Dup is not a

Re: Confused about const

2010-03-20 Thread Paul D. Anderson
After further review, I now realize that the right way (for me) to do this is to add a .dup property. So instead of S foo(const S s) { S other; other = s; } or S foo(const S s) { S other = S(s); return other; } which didn't look right to me, I can write S foo(const S s) {

Re: Confused about const

2010-03-20 Thread Steven Schveighoffer
On Fri, 19 Mar 2010 20:58:21 -0400, Paul D. Anderson wrote: I created a struct, call it "S", and some functions that operate on S. But I'm confused about when const is useful. Being an old Java programmer, I use 'const' the same as I used 'final' in Java. So most of my functions look lik

Re: Confused about const

2010-03-20 Thread bearophile
Paul D. Anderson: > My struct has a dynamic array as a member -- that seems to be the problem. > This code doesn't compile: > > struct S { > int x; > int[] a; > } > > S foo(const S b) { > S other = b; > return other; > } > void main() {} This compiles, oh joy: struct S { int x;

Re: Confused about const

2010-03-20 Thread Paul D. Anderson
bearophile Wrote: > Paul D. Anderson: > > My struct has a dynamic array as a member -- that seems to be the problem. > > This code doesn't compile: > > > > struct S { > > int x; > > int[] a; > > } > > > > S foo(const S b) { > > S other = b; > > return other; > > } > > void main() {} >

Re: Confused about const

2010-03-20 Thread bearophile
So the question here is: do you know if there are important use cases for a shallow_const attribute? Bye, bearophile

Re: Confused about const

2010-03-20 Thread bearophile
> Transitive const requires transitive copy, it's easy :-) Or when possible, you don't copy it, just return it. Bye, bearophile

Re: Confused about const

2010-03-20 Thread bearophile
biozic: > I'm not sure if this is completely right nor if I'm completely clear, > though... :) I think you are right. In practice to copy something const to something that's not const you need a deep copy function, because inside the array there can be other arrays that are const, etc. Transiti

Re: Confused about const

2010-03-20 Thread biozic
My guess is that: struct S { int x; int[] a; } S foo(const S b) { foo makes the promise that b's members won't change: so 'b.x' is implicitely const(int) and 'b.a' is implicitely const(int[]). S other = b; Here you assign 'b.x', which is const(int), to 'other.x', which is int.

Re: Confused about const

2010-03-20 Thread Paul D. Anderson
bearophile Wrote: > Paul D. Anderson: > > S x = cast(S) a; > > S y = cast(S) b; > > In Java (especially old Java) casts may be common, but in D they are > something that has to be used with care, don't cast away things carelessly :-) > > Bye, > bearophile I realize that casting is a la

Re: Confused about const

2010-03-20 Thread Paul D. Anderson
bearophile Wrote: > Paul D. Anderson: > > > I created a struct, call it "S", and some functions that operate on S. But > > I'm confused about when const is useful. > > > > Being an old Java programmer, I use 'const' the same as I used 'final' in > > Java. So most of my functions look like this

Re: Confused about const

2010-03-20 Thread bearophile
Paul D. Anderson: > S x = cast(S) a; > S y = cast(S) b; In Java (especially old Java) casts may be common, but in D they are something that has to be used with care, don't cast away things carelessly :-) Bye, bearophile

Re: Confused about const

2010-03-20 Thread bearophile
Paul D. Anderson: > I created a struct, call it "S", and some functions that operate on S. But > I'm confused about when const is useful. > > Being an old Java programmer, I use 'const' the same as I used 'final' in > Java. So most of my functions look like this: > > S for(const S a, const S b

Confused about const

2010-03-20 Thread Paul D. Anderson
I created a struct, call it "S", and some functions that operate on S. But I'm confused about when const is useful. Being an old Java programmer, I use 'const' the same as I used 'final' in Java. So most of my functions look like this: S for(const S a, const S b) { S x = a; S y = b;