const AB c = {a,20, numbers};

bearophile bearophileHUGS at lycos.com
Mon Apr 16 09:49:42 PDT 2012


sclytrack:

> struct AB
> {
> 	int a;
> 	int b;
> 	int [] numbers;
> }
>
> int main()
> {
> 	int a = 300;
> 	const int [] numbers = new int[2];
> 	const AB c = {a,20, numbers};		// line 66
>     writeln(c);
> 	return 0;
> }
>
> ----------------------------
> -debug
> -unittest
>
> src/main.d(66): Error: cannot implicitly convert expression 
> (numbers) of type const(int[]) to int[]
>
> ---------------------------
>
>
> const AB c;
>
> 	typeof(c.a)	is  const(int);
> 	typeof(c.numbers) is const(int []);
>
>
> Shouldn't the code above accept the const(int [])   ?

I think you are asking too much to the poor type system. You are 
giving a const dynamic array (that's not a value) to assign it to 
a mutable variable, and then you want to assign the result to a 
const value. The type system is not able to perform those jumps.

But this seems to work:

struct AB {
     int a, b;
     int [] numbers;
}
void main() {
     import std.stdio;
     int a = 300;
     const numbers = new int[2];
     const c = const(AB)(a, 20, numbers);
     writeln(c);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list