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: How do you put log calls in constructors when they may be created in a static context?

2018-08-09 Thread aliak via Digitalmars-d-learn
On Thursday, 9 August 2018 at 20:02:41 UTC, Jonathan M Davis 
wrote:
On Thursday, August 9, 2018 7:15:58 AM MDT aliak via 
Digitalmars-d-learn wrote:

[...]


It's failing, because you got the condition backwards. __ctfe 
is true during CTFE and false during runtime. Your code has


if(__ctfe)
writeln("log it");

which means that it will attempt to run writeln at compile 
time, whereas if you used


if(!__ctfe)
writeln("log it");

it would skip it at compile time. The problem isn't that 
writeln is being compiled in. The problem is that it's being 
encountered when CTFE is running the code. So, a static if is 
unnecessary. You just need to get the condition right.


- Jonathan M Davis


Haha doh! Backwards it was. Thanks!


vibe.d: Finding out if currently in webinterface request

2018-08-09 Thread Johannes Loher via Digitalmars-d-learn
I already posted this in the vibe.d forums 
(https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here:


Is there a way to find out, if we are currently in a webinterface 
request?


My usecase is the following:

I want to implement a logger, which in addition to the actual log 
message also logs information about the request which is 
currently being handled (if any). For this I would like to call 
request(), which gives me the current HTTPServerRequest. However, 
this results in an AssertError, whenever logging appears outside 
of a webinterface request (so basically immediately after 
starting the application, because vibe.d logs some things 
automatically). So I would need to check, if we are currently 
processing a webinterface request.


Another usecase which would be interesting to me and which has 
the exact same problem is the following:


Suppose in my received request, there is an HTTP header 
X-Correlation-Id: . Now whenever during processing of this 
request I start an HTTP request to some other service (e.g. via 
requestHTTP()), I also want to include this header in the new 
request I send. Of course, you could implement this by always 
passing the header manually, but I'd prefer to implement this 
using a generalized wrapper around requestHTTP(). But then in 
this wrapper, I would also need to call request(), which makes it 
impossible to call it from outside of webinterface request (e.g. 
it could be triggered by a timer, or something like this). But it 
would be nice, to be able to just use a single function for all 
my requests (to keep things uniform).


Re: How do you put log calls in constructors when they may be created in a static context?

2018-08-09 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, August 9, 2018 7:15:58 AM MDT aliak via Digitalmars-d-learn 
wrote:
> On Thursday, 9 August 2018 at 12:01:42 UTC, Jonathan M Davis
>
> wrote:
> > On Thursday, August 9, 2018 2:37:49 AM MDT aliak via
> >
> > Digitalmars-d-learn wrote:
> >> On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis
> >>
> >> wrote:
> >> > On Wednesday, August 8, 2018 3:54:34 PM MDT aliak via
> >> >
> >> > Digitalmars-d-learn wrote:
> >> >> I'm trying to debug stuff, so I want to add verbose logging
> >> >>
> >> >> struct S(T) {
> >> >>
> >> >>this() {
> >> >>
> >> >>  writeln("created S(T) with properties and ID");
> >> >>
> >> >>}
> >> >>
> >> >> }
> >> >>
> >> >> static a = S!int(); // bah
> >> >>
> >> >> I guess users can call this code from any context, but when
> >> >> i'd also like to see the log output for debugging purposes.
> >> >> Is there a way around this?
> >> >>
> >> >> Can I maybe only do a writeln in a non compile-time context?
> >> >
> >> > if(__ctfe)
> >> > {
> >> >
> >> > // code here will execute if this is encountered during
> >> >
> >> > CTFE
> >> >
> >> > }
> >> > else
> >> > {
> >> >
> >> > // code here will execute if this is encountered outside
> >> >
> >> > of
> >> >
> >> > CTFE
> >> > }
> >> >
> >> > - Jonathan M Davis
> >>
> >> That won't work because __ctfe is not readable at compile
> >> time. And I don't want that writeln there when there's compile
> >> time evaluation because errors.
> >
> > Huh? __ctfe's entire purpose is so that you can differentiate
> > between code that's run at compile-time and code that's run at
> > runtime. If a piece of code is executed at compile-time, __ctfe
> > is true, whereas if it's executed at runtime, __ctfe is false.
> > So, if you have
> >
> > this(T i)
> > {
> >
> > if(!__ctfe)
> >
> > writeln("log it");
> >
> > }
> >
> > then the code will print "log it" if the object is constructed
> > at runtime, whereas it won't print anything if it's run at
> > compile time, and there won't be any errors for trying to call
> > writeln at compile time, because it will have been skipped.
> >
> > - Jonathan M Davis
>
> Maybe I'm just explaining what I'm trying to do wrong, but that
> code doesn't compile when "this" is called in a valid compile
> time evaluation context, you'll get errors:
> https://run.dlang.io/is/KiJrR1
>
> That link contains the gist of what I want to do. If you could
> static if (__ctfe) ...  then that'd work but I don't know if
> there's something like that. Alternatively, if I can get a non
> compile time evaluated static then that also will do the trick.

It's failing, because you got the condition backwards. __ctfe is true during
CTFE and false during runtime. Your code has

if(__ctfe)
writeln("log it");

which means that it will attempt to run writeln at compile time, whereas if
you used

if(!__ctfe)
writeln("log it");

it would skip it at compile time. The problem isn't that writeln is being
compiled in. The problem is that it's being encountered when CTFE is running
the code. So, a static if is unnecessary. You just need to get the condition
right.

- Jonathan M Davis





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: How do you put log calls in constructors when they may be created in a static context?

2018-08-09 Thread aliak via Digitalmars-d-learn
On Thursday, 9 August 2018 at 12:01:42 UTC, Jonathan M Davis 
wrote:
On Thursday, August 9, 2018 2:37:49 AM MDT aliak via 
Digitalmars-d-learn wrote:

On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis

wrote:
> On Wednesday, August 8, 2018 3:54:34 PM MDT aliak via
>
> Digitalmars-d-learn wrote:
>> I'm trying to debug stuff, so I want to add verbose logging
>>
>> struct S(T) {
>>
>>this() {
>>
>>  writeln("created S(T) with properties and ID");
>>
>>}
>>
>> }
>>
>> static a = S!int(); // bah
>>
>> I guess users can call this code from any context, but when 
>> i'd also like to see the log output for debugging purposes. 
>> Is there a way around this?

>>
>> Can I maybe only do a writeln in a non compile-time context?
>
> if(__ctfe)
> {
>
> // code here will execute if this is encountered during 
> CTFE

>
> }
> else
> {
>
> // code here will execute if this is encountered outside 
> of

>
> CTFE
> }
>
> - Jonathan M Davis

That won't work because __ctfe is not readable at compile 
time. And I don't want that writeln there when there's compile 
time evaluation because errors.


Huh? __ctfe's entire purpose is so that you can differentiate 
between code that's run at compile-time and code that's run at 
runtime. If a piece of code is executed at compile-time, __ctfe 
is true, whereas if it's executed at runtime, __ctfe is false. 
So, if you have


this(T i)
{
if(!__ctfe)
writeln("log it");
}

then the code will print "log it" if the object is constructed 
at runtime, whereas it won't print anything if it's run at 
compile time, and there won't be any errors for trying to call 
writeln at compile time, because it will have been skipped.


- Jonathan M Davis


Maybe I'm just explaining what I'm trying to do wrong, but that 
code doesn't compile when "this" is called in a valid compile 
time evaluation context, you'll get errors: 
https://run.dlang.io/is/KiJrR1


That link contains the gist of what I want to do. If you could 
static if (__ctfe) ...  then that'd work but I don't know if 
there's something like that. Alternatively, if I can get a non 
compile time evaluated static then that also will do the trick.




Re: How do you put log calls in constructors when they may be created in a static context?

2018-08-09 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, August 9, 2018 2:37:49 AM MDT aliak via Digitalmars-d-learn 
wrote:
> On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis
>
> wrote:
> > On Wednesday, August 8, 2018 3:54:34 PM MDT aliak via
> >
> > Digitalmars-d-learn wrote:
> >> I'm trying to debug stuff, so I want to add verbose logging
> >>
> >> struct S(T) {
> >>
> >>this() {
> >>
> >>  writeln("created S(T) with properties and ID");
> >>
> >>}
> >>
> >> }
> >>
> >> static a = S!int(); // bah
> >>
> >> I guess users can call this code from any context, but when
> >> i'd also like to see the log output for debugging purposes. Is
> >> there a way around this?
> >>
> >> Can I maybe only do a writeln in a non compile-time context?
> >
> > if(__ctfe)
> > {
> >
> > // code here will execute if this is encountered during CTFE
> >
> > }
> > else
> > {
> >
> > // code here will execute if this is encountered outside of
> >
> > CTFE
> > }
> >
> > - Jonathan M Davis
>
> That won't work because __ctfe is not readable at compile time.
> And I don't want that writeln there when there's compile time
> evaluation because errors.

Huh? __ctfe's entire purpose is so that you can differentiate between code
that's run at compile-time and code that's run at runtime. If a piece of
code is executed at compile-time, __ctfe is true, whereas if it's executed
at runtime, __ctfe is false. So, if you have

this(T i)
{
if(!__ctfe)
writeln("log it");
}

then the code will print "log it" if the object is constructed at runtime,
whereas it won't print anything if it's run at compile time, and there won't
be any errors for trying to call writeln at compile time, because it will
have been skipped.

- Jonathan M Davis





Re: How do you put log calls in constructors when they may be created in a static context?

2018-08-09 Thread aliak via Digitalmars-d-learn
On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis 
wrote:
On Wednesday, August 8, 2018 3:54:34 PM MDT aliak via 
Digitalmars-d-learn wrote:

I'm trying to debug stuff, so I want to add verbose logging

struct S(T) {
   this() {
 writeln("created S(T) with properties and ID");
   }
}

static a = S!int(); // bah

I guess users can call this code from any context, but when 
i'd also like to see the log output for debugging purposes. Is 
there a way around this?


Can I maybe only do a writeln in a non compile-time context?


if(__ctfe)
{
// code here will execute if this is encountered during CTFE
}
else
{
// code here will execute if this is encountered outside of 
CTFE

}

- Jonathan M Davis


That won't work because __ctfe is not readable at compile time. 
And I don't want that writeln there when there's compile time 
evaluation because errors.


1) I want to be able to log when a type is created
2) I want to declare a locally static runtime type

Ie:

import std.stdio;

struct S(T) {
T i;
this(T i) {
this.i = i;
writeln("log it");
}
}

int f() {
static x = S!int(3);
return x.i++;
}

void main() {
writeln(f); // print 3
writeln(f); // print 4
}