www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Filling an array

reply Alex <sascha.orlov gmail.com> writes:
Hi all!
I have, maybe, a silly question..
Not sure, if
https://forum.dlang.org/post/ibxhuqamgclrcatsyhst forum.dlang.org
has something to do with the topic

Having the following code:

import std.typecons;
import std.algorithm;

void main()
{
     uint[] arr_ref;
     arr_ref.length = 5;
     assert(arr_ref == [0, 0, 0, 0, 0]);
     arr_ref[] = 1;
     assert(arr_ref == [1, 1, 1, 1, 1]);

     Nullable!uint[] arr;
     arr.length = 5;
     bool[] check_arr;
     arr.each!(a => check_arr ~= a.isNull);
     assert(check_arr == [true, true, true, true, true]);

     //arr[] = 1;
     fill(arr, 1);
     assert(arr == [1, 1, 1, 1, 1]);
}

The question is, why the commented out line throws the error:
Error: cannot implicitly convert expression (1) of type int to 
Nullable!uint[],
while the line after that works.
Mar 12 2016
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:

     //arr[] = 1;

 The question is, why the commented out line throws the error:
 Error: cannot implicitly convert expression (1) of type int to 
 Nullable!uint[],
 while the line after that works.
Looks like a bug somewhere. The work around is to cast: arr[] = cast(Nullable!uint)1; I suggest you file this in the bug tracker [1] if it isn't there already. Just use he minimal code that shows the problem: void main() { import std.typecons; Nullable!uint[] arr; arr.length = 5; arr[] = 1; } [1] https://dlang.org/bugstats.php
Mar 12 2016
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Saturday, 12 March 2016 at 15:44:00 UTC, Mike Parker wrote:
 On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:

     //arr[] = 1;

 The question is, why the commented out line throws the error:
 Error: cannot implicitly convert expression (1) of type int to 
 Nullable!uint[],
 while the line after that works.
Looks like a bug somewhere. The work around is to cast: arr[] = cast(Nullable!uint)1; I suggest you file this in the bug tracker [1] if it isn't there already. Just use he minimal code that shows the problem: void main() { import std.typecons; Nullable!uint[] arr; arr.length = 5; arr[] = 1; } [1] https://dlang.org/bugstats.php
Thanks! Bug filed under https://issues.dlang.org/show_bug.cgi?id=15792
Mar 12 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 12.03.2016 16:44, Mike Parker wrote:
 arr[] = cast(Nullable!uint)1;
Nicer than a cast: construct a Nullable!int. arr[] = Nullable!uint(1);
Mar 12 2016
parent Alex <sascha.orlov gmail.com> writes:
On Saturday, 12 March 2016 at 19:35:30 UTC, ag0aep6g wrote:
 On 12.03.2016 16:44, Mike Parker wrote:
 arr[] = cast(Nullable!uint)1;
Nicer than a cast: construct a Nullable!int. arr[] = Nullable!uint(1);
ok... so... this makes the error very strange, then... almost senseless...
Mar 12 2016
prev sibling parent reply user42 <user42 invalid.email> writes:
On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:
 /snip
I thought this was supposed to halt with an error rather than compile and set all members to 1. The syntax, to me anyways, doesn't really communicate the intention of: set all members to 1.
     //arr[] = 1;
Whereas the following does
     fill(arr, 1);
Mar 12 2016
parent reply Alex <sascha.orlov gmail.com> writes:
On Saturday, 12 March 2016 at 16:37:25 UTC, user42 wrote:
 On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:
 /snip
I thought this was supposed to halt with an error rather than compile and set all members to 1. The syntax, to me anyways, doesn't really communicate the intention of: set all members to 1.
     //arr[] = 1;
Whereas the following does
     fill(arr, 1);
Well, this was not the question. As stated here: https://dlang.org/spec/arrays.html in the section "array setting", it is possible to set an array in such a manner. And my question was, why a specific array behaves not as expected. So, either there is a problem with filling an array, or, there is a problem with implicit conversion of a Nullable!T to its underlying type.
Mar 12 2016
parent user42 <0 0.0> writes:
On Saturday, 12 March 2016 at 18:33:16 UTC, Alex wrote:
 On Saturday, 12 March 2016 at 16:37:25 UTC, user42 wrote:
 On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:
 /snip
I thought this was supposed to halt with an error rather than compile and set all members to 1. The syntax, to me anyways, doesn't really communicate the intention of: set all members to 1.
     //arr[] = 1;
Whereas the following does
     fill(arr, 1);
Well, this was not the question. As stated here: https://dlang.org/spec/arrays.html in the section "array setting", it is possible to set an array in such a manner. And my question was, why a specific array behaves not as expected. So, either there is a problem with filling an array, or, there is a problem with implicit conversion of a Nullable!T to its underlying type.
Learned something new. I guess I missed that detail when I read that page.
Mar 13 2016