www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - variable x cannot be read at compile time - how to get around this?

reply "Sergey" <httpal gmail.com> writes:
   Hello everyone!

I need to create a two-dimensional array in this way, for example:

auto x = 10;
auto y = 10;
auto some_array = new string[x][y];
variable x cannot be read at compile time

I tried this:
enum columns_array = 
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
auto y = 10;
int i = 1;
auto some_array = new string[columns_array[i]][y];
Error: columns_array is used as a type

And yet, if I have a function:
string[x][] some_function (some par) {
    auto x = 10;
    auto y = 10;
    auto some_array = new string[x][y];
    return some_array;
    }

Thanks in advance.
Nov 12 2014
next sibling parent reply "Brian Schott" <briancschott gmail.com> writes:
On Thursday, 13 November 2014 at 07:08:19 UTC, Sergey wrote:
   Hello everyone!

 I need to create a two-dimensional array in this way, for 
 example:
What did you want the type of the array to be? "string[10][10]" or "string[][]"?
Nov 12 2014
parent "Sergey" <httpal gmail.com> writes:
On Thursday, 13 November 2014 at 07:50:26 UTC, Brian Schott wrote:
 On Thursday, 13 November 2014 at 07:08:19 UTC, Sergey wrote:
  Hello everyone!

 I need to create a two-dimensional array in this way, for 
 example:
What did you want the type of the array to be? "string[10][10]" or "string[][]"?
Oops, I did not see some of the details, how to work with string[][]. Right now I still try to understand. Sorry. Thanks for the push! :)
Nov 13 2014
prev sibling next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thu, 13 Nov 2014 07:08:17 +0000
Sergey via Digitalmars-d <digitalmars-d puremagic.com> wrote:

    Hello everyone!
=20
 I need to create a two-dimensional array in this way, for example:
=20
 auto x =3D 10;
 auto y =3D 10;
 auto some_array =3D new string[x][y];
 variable x cannot be read at compile time
=20
 I tried this:
 enum columns_array =3D=20
 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
 auto y =3D 10;
 int i =3D 1;
 auto some_array =3D new string[columns_array[i]][y];
 Error: columns_array is used as a type
=20
 And yet, if I have a function:
 string[x][] some_function (some par) {
     auto x =3D 10;
     auto y =3D 10;
     auto some_array =3D new string[x][y];
     return some_array;
     }
=20
 Thanks in advance.
you can't. use static constructor.
Nov 12 2014
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/13/14 2:08 AM, Sergey wrote:
    Hello everyone!

 I need to create a two-dimensional array in this way, for example:

 auto x = 10;
 auto y = 10;
 auto some_array = new string[x][y];
auto some_array = new string[][](x, y); Note, this creates 10 arrays of 10 elements all on the heap, and then a 10 element array to point at them. If you wanted an array of 10 *fixed sized* arrays (which is what your code was trying to do), then you need to have the first dimension be a compile-time constant such as a literal or an enum/immutable. -Steve
Nov 13 2014
parent "Sergey" <httpal gmail.com> writes:
On Thursday, 13 November 2014 at 14:27:32 UTC, Steven 
Schveighoffer wrote:
 On 11/13/14 2:08 AM, Sergey wrote:
   Hello everyone!

 I need to create a two-dimensional array in this way, for 
 example:

 auto x = 10;
 auto y = 10;
 auto some_array = new string[x][y];
auto some_array = new string[][](x, y); Note, this creates 10 arrays of 10 elements all on the heap, and then a 10 element array to point at them. If you wanted an array of 10 *fixed sized* arrays (which is what your code was trying to do), then you need to have the first dimension be a compile-time constant such as a literal or an enum/immutable. -Steve
Thanks!!! This is what I need! auto some_array = new string[][](x, y);
Nov 13 2014