www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - can't access an alias created inside an if statement

reply Flade <consolemaster0 gmail.com> writes:
I have used an if-else statement to create an alias to avoid code 
duplication but it doesn't let me access it outside the if 
statement. Is there a way to solve this?
Aug 05 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 5 August 2020 at 09:05:36 UTC, Flade wrote:
 I have used an if-else statement to create an alias to avoid 
 code duplication but it doesn't let me access it outside the if 
 statement. Is there a way to solve this?
You're probably looking for static if: static if (useAlias) { alias myAlias = getAlias!(); } myAlias foo = getFoo(); What happens is a regular if statement introduces a scope, so anything declared inside it is unavailable outside. static if does not introduce a new scope, and so its contents can be accessed. static if only works with compile-time constant conditions, but aliases are also compile-time constructs, so this should not pose a problem. -- Simen
Aug 05 2020
parent reply Flade <consolemaster0 gmail.com> writes:
On Wednesday, 5 August 2020 at 09:25:23 UTC, Simen Kjærås wrote:
 On Wednesday, 5 August 2020 at 09:05:36 UTC, Flade wrote:
 I have used an if-else statement to create an alias to avoid 
 code duplication but it doesn't let me access it outside the 
 if statement. Is there a way to solve this?
You're probably looking for static if: static if (useAlias) { alias myAlias = getAlias!(); } myAlias foo = getFoo(); What happens is a regular if statement introduces a scope, so anything declared inside it is unavailable outside. static if does not introduce a new scope, and so its contents can be accessed. static if only works with compile-time constant conditions, but aliases are also compile-time constructs, so this should not pose a problem. -- Simen
Thanks! You see it should work but the thing is. I'm using it inside a function. I'm checking for one of the function's parameter (if parameter == false) and it says that "the variable `parameter` cannot be read at compile time. Do you know if there is a way to fix this?
Aug 05 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 5 August 2020 at 09:32:58 UTC, Flade wrote:
 Thanks! You see it should work but the thing is. I'm using it 
 inside a function. I'm checking for one of the function's 
 parameter (if parameter == false) and it says that "the 
 variable `parameter` cannot be read at compile time. Do you 
 know if there is a way to fix this?
As the error message says, the value must be known at compile time. Most likely, you can simply pass it as a template parameter: void fun(bool parameter)(int arg1, string arg2) { static if (parameter) { } } void main() { fun!true(1, "foo"); fun!false(19, "bar"); } -- Simen
Aug 05 2020
parent Flade <consolemaster0 gmail.com> writes:
On Wednesday, 5 August 2020 at 09:39:47 UTC, Simen Kjærås wrote:
 On Wednesday, 5 August 2020 at 09:32:58 UTC, Flade wrote:
 Thanks! You see it should work but the thing is. I'm using it 
 inside a function. I'm checking for one of the function's 
 parameter (if parameter == false) and it says that "the 
 variable `parameter` cannot be read at compile time. Do you 
 know if there is a way to fix this?
As the error message says, the value must be known at compile time. Most likely, you can simply pass it as a template parameter: void fun(bool parameter)(int arg1, string arg2) { static if (parameter) { } } void main() { fun!true(1, "foo"); fun!false(19, "bar"); } -- Simen
Thanks man! Works as expected! Have a great day!
Aug 05 2020