Re: variable _param_0 cannot be read at compile time

2018-08-09 Thread learnfirst1 via Digitalmars-d-learn

On Thursday, 9 August 2018 at 13:42:03 UTC, Kagamin wrote:

struct M {
int i;
S*[100] s;
}
struct S {
M* mp;
bool x;
}

S* add(A...)() {
alias m = A[0];
__gshared s = S(,A[1..$]);
m.s[m.i++] = 
return 
}

void main(){
__gshared M m = M(0);
__gshared S s = S(, false);
m.s[m.i++] = 
auto p = add!(m, true);
}


this is what I want! thanks.



Re: variable _param_0 cannot be read at compile time

2018-08-09 Thread Kagamin via Digitalmars-d-learn

struct M {
int i;
S*[100] s;
}
struct S {
M* mp;
bool x;
}

S* add(A...)() {
alias m = A[0];
__gshared s = S(,A[1..$]);
m.s[m.i++] = 
return 
}

void main(){
__gshared M m = M(0);
__gshared S s = S(, false);
m.s[m.i++] = 
auto p = add!(m, true);
}


Re: variable _param_0 cannot be read at compile time

2018-08-08 Thread learnfirst1 via Digitalmars-d-learn

On Wednesday, 8 August 2018 at 13:13:42 UTC, Simen Kjærås wrote:

On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote:

Why this is a error ?

```
struct S {
bool v;
string x;
}

S* add(A...)(ref A a) {
__gshared s = S(a);
return 
}

void main(){
auto p = add(true);
}
```

test.d(9): Error: variable _param_0 cannot be read at compile 
time


__gshared and static need to be initialized with a value known 
at compile-time. You're trying to give it a run-time value. You 
can set this after it's first created:


S* add(A...)(ref A a) {
__gshared S s;
s = S(a);
return 
}

That's a little kludgy, but apparently that's how it be.

You also get another error message:
Error: function test.add!bool.add(ref bool _param_0) is not 
callable using argument types (bool)


That's because add takes its arguments by ref, and true is a 
literal that has no canonical address, and thus can't be passed 
by ref. You might consider using auto ref.


--
  Simen


is there a way to pass A a... as alias into template ? so the 
code can be just like this:


__gshared S s = S(, false);


the binary size is short since in this case the s is static build 
int. and run a bit fast.




Re: variable _param_0 cannot be read at compile time

2018-08-08 Thread Simen Kjærås via Digitalmars-d-learn

On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote:

Why this is a error ?

```
struct S {
bool v;
string x;
}

S* add(A...)(ref A a) {
__gshared s = S(a);
return 
}

void main(){
auto p = add(true);
}
```

test.d(9): Error: variable _param_0 cannot be read at compile 
time


__gshared and static need to be initialized with a value known at 
compile-time. You're trying to give it a run-time value. You can 
set this after it's first created:


S* add(A...)(ref A a) {
__gshared S s;
s = S(a);
return 
}

That's a little kludgy, but apparently that's how it be.

You also get another error message:
Error: function test.add!bool.add(ref bool _param_0) is not 
callable using argument types (bool)


That's because add takes its arguments by ref, and true is a 
literal that has no canonical address, and thus can't be passed 
by ref. You might consider using auto ref.


--
  Simen


Re: variable _param_0 cannot be read at compile time

2018-08-08 Thread learnfirst1 via Digitalmars-d-learn

On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote:

Why this is a error ?



this code example can explain what I am try to do here:

struct M {
int i;
S*[100] s;
}
struct S {
M* mp;
bool x;
}


S* add(A...)(ref A a) {
__gshared s = S(a);
alias m = a[0];
m.s[m.i++] = 
return 
}

void main(){
__gshared M m = M(0);
__gshared S s = S(, false);
m.s[m.i++] = 
auto p = add(, true);  // variable _param_0 cannot be 
read at compile time

}

because S has the optional ctor parameters, I can not use 
template alias param here( some how not work) .