www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - front doesn't compile for arrays of immutable data

reply "Roman D. Boiko" <rb d-coding.com> writes:
import std.range;
struct Element {
   //immutable // uncomment to break compilation
    int _i;
   this(int i) { _i = i; }
}
void main() {
   auto arr = [Element.init];
   arr.front;
}

Declaring _i immutable yields the following compilation error:

/usr/include/x86_64-linux-gnu/dmd/phobos/std/array.d(460): Error: 
a[0LU] isn't mutable
sample.d(11): Error: template instance std.array.front!(Element) 
error instantiating

Is this a bug, or I misunderstand something?
Jun 25 2012
next sibling parent reply "Roman D. Boiko" <rb d-coding.com> writes:
 property ref T front(T)(T[] a)
if (!isNarrowString!(T[]) && !is(T[] == void[]))
{
     assert(a.length, "Attempting to fetch the front of an empty 
array of " ~
            typeof(a[0]).stringof);
     return a[0];
}

Why is front returned by ref even when it is not possible to do 
so?
Jun 25 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 06/25/2012 04:27 PM, Roman D. Boiko wrote:
  property ref T front(T)(T[] a)
 if (!isNarrowString!(T[]) && !is(T[] == void[]))
 {
 assert(a.length, "Attempting to fetch the front of an empty array of " ~
 typeof(a[0]).stringof);
 return a[0];
 }

 Why is front returned by ref even when it is not possible to do so?

It should always be possible to do so. What you are experiencing seems to be a compiler bug.
Jun 25 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, June 25, 2012 16:27:20 Roman D. Boiko wrote:
  property ref T front(T)(T[] a)
 if (!isNarrowString!(T[]) && !is(T[] == void[]))
 {
      assert(a.length, "Attempting to fetch the front of an empty
 array of " ~
             typeof(a[0]).stringof);
      return a[0];
 }
 
 Why is front returned by ref even when it is not possible to do
 so?

What do you mean that it's not possible? What's not possible about it? - Jonathan M Davis
Jun 25 2012
prev sibling next sibling parent "Roman D. Boiko" <rb d-coding.com> writes:
On Monday, 25 June 2012 at 15:39:07 UTC, Jonathan M Davis wrote:
 On Monday, June 25, 2012 16:27:20 Roman D. Boiko wrote:
  property ref T front(T)(T[] a)
 if (!isNarrowString!(T[]) && !is(T[] == void[]))
 {
      assert(a.length, "Attempting to fetch the front of an 
 empty
 array of " ~
             typeof(a[0]).stringof);
      return a[0];
 }
 
 Why is front returned by ref even when it is not possible to do
 so?

What do you mean that it's not possible? What's not possible about it? - Jonathan M Davis

Looks like I came to wrong conclusions, so my statement from second post should be ignored. The first post shows the problem.
Jun 25 2012
prev sibling next sibling parent "Roman D. Boiko" <rb d-coding.com> writes:
On Monday, 25 June 2012 at 15:38:09 UTC, Timon Gehr wrote:
 It should always be possible to do so. What you are 
 experiencing seems to be a compiler bug.

I couldn't find it reported before. The closest are: d.puremagic.com/issues/show_bug.cgi?id=6480 http://d.puremagic.com/issues/show_bug.cgi?id=7258 I guess I should report it?
Jun 25 2012
prev sibling next sibling parent "Roman D. Boiko" <rb d-coding.com> writes:
On Monday, 25 June 2012 at 15:58:14 UTC, Roman D. Boiko wrote:
 On Monday, 25 June 2012 at 15:38:09 UTC, Timon Gehr wrote:
 It should always be possible to do so. What you are 
 experiencing seems to be a compiler bug.

I couldn't find it reported before. The closest are: http://d.puremagic.com/issues/show_bug.cgi?id=6480 http://d.puremagic.com/issues/show_bug.cgi?id=7258 I guess I should report it?

Jun 25 2012
prev sibling parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Monday, 25 June 2012 at 14:23:25 UTC, Roman D. Boiko wrote:
 import std.range;
 struct Element {
   //immutable // uncomment to break compilation
    int _i;
   this(int i) { _i = i; }
 }
 void main() {
   auto arr = [Element.init];
   arr.front;
 }

 Declaring _i immutable yields the following compilation error:

 /usr/include/x86_64-linux-gnu/dmd/phobos/std/array.d(460): 
 Error: a[0LU] isn't mutable
 sample.d(11): Error: template instance 
 std.array.front!(Element) error instantiating

 Is this a bug, or I misunderstand something?

This is a bug of the compiler, and it is already filed. http://d.puremagic.com/issues/show_bug.cgi?id=6336 I've already posted a pull request to fix the bug, but it has been hold a while. Kenji Hara
Jun 25 2012