Reputation: 5443
I would like to have a C# runtime expression which always evaluates to true, but which the compiler does not recognize as a constant. (Motivation below) There are plenty of ways to achieve this, but I'm wondering if there's a nice most-idiomatic one. The one I'm using now is just bool b = MyMathLibrary.Sin(0f) < 100f;
. Performance is not a concern.
Motivation:
I have some code which should only execute on a certain platform.
The natural way to code this up would be something like
#if IS_COMPILING_FOR_SPECIAL_PLATFORM
int bar = 123;
foo(bar);
#endif
However, I'm developing in Unity, and when developing the code, Visual Studio is not configured for IS_COMPILING_FOR_SPECIAL_PLATFORM, which means that the code in the #IF is not compiled. This introduces development slowdown and maintenance overhead when something which foo() depends on changes. ie, instead of getting a compilation error right away, I have to wait for the larger and slower build pipeline.
So instead I'd like to do something like
#if IS_COMPILING_FOR_SPECIAL_PLATFORM
bool isSpecialPlatform = true;
#else
bool isSpecialPlatform = false;
#endif
if (isSpecialPlatform) {
int bar = 123;
foo(bar);
}
I'd be happy with this, except I get compiler warnings about 1) Unreachable code and 2) Unused variable bar (because it's in unreachable code I assume).
I can squelch the Unreachable Code warning with a #pragma warning disable
, but I'd prefer not to introduce a blanket warning-disable for the unused variables in the code section, because that would squelch any legitimate warnings in the future.
I expect there will be at least one "you're asking the wrong question"-type reply, and I'm looking forward to hearing those suggestions as well.
Upvotes: 0
Views: 87