How do you put log calls in constructors when they may be created in a static context?
aliak
something at something.com
Thu Aug 9 08:37:49 UTC 2018
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
}
More information about the Digitalmars-d-learn
mailing list