Re: Assigning a static array

2013-04-19 Thread bearophile
Steven Schveighoffer: we increasingly need scope to work for D to be easy to avoid mistakes. I have added a note about scope: http://d.puremagic.com/issues/show_bug.cgi?id=5212 Bye, bearophile

Re: Assigning a static array

2013-04-19 Thread bearophile
Steven Schveighoffer: we increasingly need scope to work for D to be easy to avoid mistakes. But I don't see any plan/implementation timeframe for "scope" :-( I don't know why. I'd like to know Hara opinion on this. Bye, bearophile

Re: Assigning a static array

2013-04-19 Thread Steven Schveighoffer
On Thu, 18 Apr 2013 19:15:06 -0400, H. S. Teoh wrote: I'm all for making this @system at the very least, if not outright compile error. Storing a persistent reference to a stack-allocated object is outright wrong... in the case of variadic array args, if the compiler can't prove that args wil

Re: Assigning a static array

2013-04-19 Thread Steven Schveighoffer
On Thu, 18 Apr 2013 18:13:14 -0400, Ali Çehreli wrote: Interesting. Personally, I would not even bother with the second one and expect the caller to simply put square brackets around the arguments: [snip] It is now safe, right? Yes, for this case it is OK. But I want a consistent in

Re: Assigning a static array

2013-04-19 Thread Steven Schveighoffer
On Thu, 18 Apr 2013 18:08:48 -0400, bearophile wrote: To avoid those bugs I have suggested the simpler possible thing: (V[] elems...) to dup the data on the heap every time. In theory if you write "(scope V[] elems...)" it will be free to not dup the data, avoiding the heap allocation and

Re: Assigning a static array

2013-04-18 Thread H. S. Teoh
On Thu, Apr 18, 2013 at 02:43:54PM -0700, Ali Çehreli wrote: > On 04/18/2013 02:06 PM, Brad Anderson wrote: > >Is this supposed to be allowed: > > > >ubyte[] a; > >ubyte[16] b; > >a = b; > >assert(a.ptr == b.ptr); > > > >Because if so that makes it terribly easy to do a bug like this (as I > >just

Re: Assigning a static array

2013-04-18 Thread Ali Çehreli
On 04/18/2013 02:54 PM, Steven Schveighoffer wrote: >> The program prints the following because the temporary arrays that are >> generated when calling the constructors are long gone: >> >> [1, 1, 1] >> [1, 1, 1] >> >> The programmer *may have* ;) expected the following output: >> >> [1, 1, 1] >>

Re: Assigning a static array

2013-04-18 Thread bearophile
Steven Schveighoffer: There is no guarantee that the incoming array from a variadic function is heap-based. But an interesting way to deal with it is that you can overload with an explicit slice parameter, and the variadic version will ONLY bind to a variadic call. For example, in dcollect

Re: Assigning a static array

2013-04-18 Thread Steven Schveighoffer
There is a similar problem with the automatically generated array arguments. The following constructor takes any number of ints that come in array form: import std.stdio; struct S { int[] a; this(int[] args...) { a = args; } void foo() {

Re: Assigning a static array

2013-04-18 Thread Brad Anderson
On Thursday, 18 April 2013 at 21:45:56 UTC, bearophile wrote: Yes, this is supposed to be allowed with the current design of D. But with the latest dmd 2.063alpha that code doesn't work, see below. [snip] Now that code gives a warning: temp.d(11): Warning: explicit slice assignment this.hash

Re: Assigning a static array

2013-04-18 Thread bearophile
Jonathan M Davis: I could see an argument that it should have to be a = b[]; so that the slicing is explicit instead of just a = b; where it's implicit, but AFAIK, that's not currently required. It's currently a warning, and it will be required. -- Ali Çehreli: T

Re: Assigning a static array

2013-04-18 Thread bearophile
Brad Anderson: Is this supposed to be allowed: ubyte[] a; ubyte[16] b; a = b; assert(a.ptr == b.ptr); Yes, this is supposed to be allowed with the current design of D. But with the latest dmd 2.063alpha that code doesn't work, see below. The Rust language removes this source of bugs becau

Re: Assigning a static array

2013-04-18 Thread Ali Çehreli
On 04/18/2013 02:06 PM, Brad Anderson wrote: Is this supposed to be allowed: ubyte[] a; ubyte[16] b; a = b; assert(a.ptr == b.ptr); Because if so that makes it terribly easy to do a bug like this (as I just saw in IRC): struct A { ubyte[] a; this(ubyte c) { ubyte[16] b;

Re: Assigning a static array

2013-04-18 Thread Jonathan M Davis
On Thursday, April 18, 2013 23:06:32 Brad Anderson wrote: > Is this supposed to be allowed: > > ubyte[] a; > ubyte[16] b; > a = b; > assert(a.ptr == b.ptr); > > Because if so that makes it terribly easy to do a bug like this > (as I just saw in IRC): > > struct A > { > ubyte[] a; > this(ubyte c)

Re: Assigning a static array

2013-04-18 Thread Brad Anderson
For reference, here was what user soos on IRC was doing that caused him to hit this : import std.digest.md; import std.stdio; struct Hash { ubyte[] hash1; ubyte[] hash2; this (string str) { auto md5 = new MD5Digest(); this.hash1 = md5.digest(str);