digitalmars.D.learn - simple static if / traits question...
- WhatMeWorry (11/11) Feb 22 2017 I'm doing conditional compilation using static ifs like so:
- Era Scarecrow (4/9) Feb 22 2017 I think you're thinking too deeply on this. During optimization
- Profile Anaysis (24/35) Feb 22 2017 You do realize that audio is a compile time constant? This means
- WhatMeForget (14/62) Feb 22 2017 Definitely overthought this one big time. But there is so much
- Profile Anaysis (12/28) Feb 23 2017 Yes, but it checks at compile time. So the code will be evaluated
- Patrick Schluter (9/28) Feb 23 2017 Or to make it simple
I'm doing conditional compilation using static ifs like so: enum bool audio = true; // if audio flag is present and set to true, add to code build static if ( (__traits(compiles, audio)) && audio) playSound(soundSys, BLEEP ); This works, but I thought there might be a simpler way. For instance, after perusing std.traits, I thought I would find something like isPresent(audio) or isSymbol(audio) templates. Or am I being obtuse here? Thanks.
Feb 22 2017
On Wednesday, 22 February 2017 at 21:27:47 UTC, WhatMeWorry wrote:I'm doing conditional compilation using static ifs like so: enum bool audio = true; // if audio flag is present and set to true, add to code build static if ( (__traits(compiles, audio)) && audio) playSound(soundSys, BLEEP );I think you're thinking too deeply on this. During optimization branches of if statements that always evaluate to false are compiled out.
Feb 22 2017
On Wednesday, 22 February 2017 at 21:27:47 UTC, WhatMeWorry wrote:I'm doing conditional compilation using static ifs like so: enum bool audio = true; // if audio flag is present and set to true, add to code build static if ( (__traits(compiles, audio)) && audio) playSound(soundSys, BLEEP ); This works, but I thought there might be a simpler way. For instance, after perusing std.traits, I thought I would find something like isPresent(audio) or isSymbol(audio) templates. Or am I being obtuse here? Thanks.You do realize that audio is a compile time constant? This means that in the binary, everything that depends on it is evaluated(as it can be, since it is known). This means that whatever app you are using will not be able to be able to "adapt" to the system changes. In this case, if the system has audio there is no way for the binary to use it because you compiled it out(if audio = false). In such a case you do not want to use a static or compile time variable unless you plan on creating multiple binaries. If your example above was just for demo then yes, you can do that but compiles is not what you want. Compiles only checks if the statement that follows is compilable as valid D code and it doesn't have anything to do with the value of the variables. There are a few options: 1. static if(audio) 2. version(audio) 3. if (audio) It looks like you are trying to create the version(audio) semantic(if exists then use, else don't). Ultimately, though, if you are trying to make a binary that can either use audio or not depending on where it is ran, you'll have to stick to using actual run time variables and probably be a bit more organized about it.
Feb 22 2017
On Wednesday, 22 February 2017 at 22:37:25 UTC, Profile Anaysis wrote:On Wednesday, 22 February 2017 at 21:27:47 UTC, WhatMeWorry wrote:Definitely overthought this one big time. But there is so much meta goodness in std.traits that I felt compelled to use "compiles" :)I'm doing conditional compilation using static ifs like so: enum bool audio = true; // if audio flag is present and set to true, add to code build static if ( (__traits(compiles, audio)) && audio) playSound(soundSys, BLEEP ); This works, but I thought there might be a simpler way. For instance, after perusing std.traits, I thought I would find something like isPresent(audio) or isSymbol(audio) templates. Or am I being obtuse here? Thanks.You do realize that audio is a compile time constant? This means that in the binary, everything that depends on it is evaluated(as it can be, since it is known). This means that whatever app you are using will not be able to be able to "adapt" to the system changes. In this case, if the system has audio there is no way for the binary to use it because you compiled it out(if audio = false). In such a case you do not want to use a static or compile time variable unless you plan on creating multiple binaries. If your example above was just for demo then yes, you can do that but compiles is not what you want. Compiles only checks if the statement that follows is compilable as valid D code and it doesn't have anything to do with the value of the variables.There are a few options: 1. static if(audio) 2. version(audio) 3. if (audio) It looks like you are trying to create the version(audio) semantic(if exists then use, else don't). Ultimately, though, if you are trying to make a binary that can either use audio or not depending on where it is ran, you'll have to stick to using actual run time variables and probably be a bit more organized about it.option 1 is the one I was shooting for. does the static if (audio) just check for the existence of audio, or does it also check to see if audio is true as well? I've got a game tutorial with 15 sequential projects. Each project introduces a new concept by building on the previous project code. But I soon had so much code duplication that I decided to use common modules/functions. The specific code could be injected via flags in the apps.d Hence my original question.
Feb 22 2017
Yes, but it checks at compile time. So the code will be evaluated by the compiler and if audio is true, it will only compile in the code in the if block. e.g, static if (audio) { do something } will be identical, in the binary, to do something if audio is true. The static if is an if statement and works like any ordinary if statement, but since you are using static(known at compile time) information then static if can actually be computed/evaluated at compile time(since all the inputs are know and cannot be changed)There are a few options: 1. static if(audio) 2. version(audio) 3. if (audio) It looks like you are trying to create the version(audio) semantic(if exists then use, else don't). Ultimately, though, if you are trying to make a binary that can either use audio or not depending on where it is ran, you'll have to stick to using actual run time variables and probably be a bit more organized about it.option 1 is the one I was shooting for. does the static if (audio) just check for the existence of audio, or does it also check to see if audio is true as well?
Feb 23 2017
On Thursday, 23 February 2017 at 18:35:29 UTC, Profile Anaysis wrote:Or to make it simple static if (audio) { do somethng } is the same thing as #if audio do something #endif in C and C++.Yes, but it checks at compile time. So the code will be evaluated by the compiler and if audio is true, it will only compile in the code in the if block. e.g, static if (audio) { do something } will be identical, in the binary, to do something if audio is true. The static if is an if statement and works like any ordinary if statement, but since you are using static(known at compile time) information then static if can actually be computed/evaluated at compile time(since all the inputs are know and cannot be changed)[...]option 1 is the one I was shooting for. does the static if (audio) just check for the existence of audio, or does it also check to see if audio is true as well?
Feb 23 2017