On Thursday, 15 May 2014 at 17:15:50 UTC, Steven Schveighoffer wrote:
Assign _items only once in the constructor.

immutable(int)[] tmp;

foreach(i; 1..4)
   tmp ~= i;

_items = tmp;

thanks for example!

On Thursday, 15 May 2014 at 17:15:50 UTC, Steven Schveighoffer wrote:
This should not work IMO.

My second example compiles successfully and displays the correct result - that's why I became interested in why it works.
you can compile yourself.

On Thursday, 15 May 2014 at 16:58:04 UTC, Yota wrote:
Here are a couple ways to initialize the array:

1:
  this() {
    import std.exception : assumeUnique;
    int[] items;
    foreach (i; 1..4) {
      items ~= i;
    }
    _items = items.assumeUnique;
  }

2:
  this() {
// The result of a pure function may be implicitly cast to immutable.
    _items = function() pure {
      int[] items;
      foreach (i; 1..4) {
        items ~= i;
      }
      return items;
    }();
  }

thanks for example!

On Thursday, 15 May 2014 at 16:58:04 UTC, Yota wrote:
As for the second example, that looks like a bug to me.

if this is actually a bug - will be good if someone could register it.

Reply via email to