digitalmars.D.learn - How do you put log calls in constructors when they may be created in a
- aliak (13/13) Aug 08 2018 I'm trying to debug stuff, so I want to add verbose logging
- Jonathan M Davis (11/22) Aug 08 2018 if(__ctfe)
- aliak (24/51) Aug 09 2018 That won't work because __ctfe is not readable at compile time.
- Jonathan M Davis (16/59) Aug 09 2018 Huh? __ctfe's entire purpose is so that you can differentiate between co...
- aliak (10/74) Aug 09 2018 Maybe I'm just explaining what I'm trying to do wrong, but that
- Jonathan M Davis (15/99) Aug 09 2018 It's failing, because you got the condition backwards. __ctfe is true du...
- aliak (3/19) Aug 09 2018 Haha doh! Backwards it was. Thanks!
- Timoses (10/23) Aug 10 2018 How about debug [1]?
- aliak (2/30) Aug 10 2018 Very nice :D Thanks!
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? Cheers, - ali
Aug 08 2018
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
Aug 08 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: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 }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
Aug 09 2018
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: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 DavisOn Wednesday, August 8, 2018 3:54:34 PM MDT aliak via Digitalmars-d-learn wrote: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.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
Aug 09 2018
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: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.On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis wrote: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 DavisOn Wednesday, August 8, 2018 3:54:34 PM MDT aliak via Digitalmars-d-learn wrote: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.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
Aug 09 2018
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: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 DavisOn Thursday, August 9, 2018 2:37:49 AM MDT aliak via Digitalmars-d-learn wrote: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.On Wednesday, 8 August 2018 at 23:47:22 UTC, Jonathan M Davis wrote: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 DavisOn Wednesday, August 8, 2018 3:54:34 PM MDT aliak via Digitalmars-d-learn wrote: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.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
Aug 09 2018
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:Haha doh! Backwards it was. Thanks![...]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
Aug 09 2018
On Wednesday, 8 August 2018 at 21:54:34 UTC, aliak 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? Cheers, - aliHow about debug [1]? struct S()T { this() { debug { writeln("Created..."); } } } and compile with the debug flag. You can also specify a debug identifier or level. [1]: https://dlang.org/spec/version.html#debug
Aug 10 2018
On Friday, 10 August 2018 at 09:29:04 UTC, Timoses wrote:On Wednesday, 8 August 2018 at 21:54:34 UTC, aliak wrote:Very nice :D Thanks!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? Cheers, - aliHow about debug [1]? struct S()T { this() { debug { writeln("Created..."); } } } and compile with the debug flag. You can also specify a debug identifier or level. [1]: https://dlang.org/spec/version.html#debug
Aug 10 2018