Thread overview
front doesn't compile for arrays of immutable data
Jun 25, 2012
Roman D. Boiko
Jun 25, 2012
Roman D. Boiko
Jun 25, 2012
Timon Gehr
Jun 25, 2012
Roman D. Boiko
Jun 25, 2012
Roman D. Boiko
Jun 25, 2012
Jonathan M Davis
Jun 25, 2012
Roman D. Boiko
Jun 26, 2012
Kenji Hara
June 25, 2012
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?

June 25, 2012
@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?
June 25, 2012
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.
June 25, 2012
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
June 25, 2012
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.
June 25, 2012
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?

June 25, 2012
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?
http://d.puremagic.com/issues/show_bug.cgi?id=8297

June 26, 2012
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