digitalmars.D.learn - Counting at compile-time
- DLearner (17/17) Jan 26 Suppose we have
- Bradley Chatha (33/34) Jan 26 It's hard to say what would exactly fit your use case since this
- DLearner (3/12) Jan 26 Thank you.
- Christian =?UTF-8?B?S8O2c3RsaW4=?= (20/54) Jan 28 for this usecase (at least the first pragmas) you could also just
- monkyyy (3/20) Jan 26 I have a compile time counter gist, it isnt no where near that
Suppose we have ``` enum Count = 0; static if (<Compile-time evaluable cond-1>) ... enum Count = Count + 1; // is disallowed else static if (<Compile-time evaluable cond-2>) ... enum Count = Count + 1; // is disallowed else ``` and the objective is to determine (at compile time) the number of times cond-1 or cond-2 is triggered (perhaps for setting size of a fixed array). So answer is zero, 1 or 2. But how to get that value into Count?
Jan 26
On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:But how to get that value into Count?It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option: ```d int myCount(bool Cond1, bool Cond2)() { // Assumes that the code is more complex, so you can't easily use something similar to: // count = cast(int)Cond1 + cast(int)Cond2; int count = 0; static if(Cond1) count++; static if(Cond2) count++; return count; } pragma(msg, myCount!(false, false)); // 0 pragma(msg, myCount!(true, false)); // 1 pragma(msg, myCount!(false, true)); // 1 pragma(msg, myCount!(true, true)); // 2 // e.g. store it in an enum enum Count = myCount!(true, false); // Or more simply... if possible for your use case enum Cond1 = true; enum Cond2 = false; enum Count = cast(int)Cond1 + cast(int)Cond2; // cast(int)true == 1; cast(int)false == 0 ``` Without additional context it's hard to give a concrete suggestion though, but I hope this helps.
Jan 26
On Sunday, 26 January 2025 at 16:43:23 UTC, Bradley Chatha wrote:On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:[...]But how to get that value into Count?It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option:Without additional context it's hard to give a concrete suggestion though, but I hope this helps.Thank you.
Jan 26
On Sunday, 26 January 2025 at 16:43:23 UTC, Bradley Chatha wrote:On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:for this usecase (at least the first pragmas) you could also just do a normal function (many "pure" functions qualify as ctfe candidates). So ```d int myCount(bool cond1, bool cond2) { int count = 0; if(cond1) { count++; } if(cond2) { count++; } return count; } ``` would also work.But how to get that value into Count?It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option: ```d int myCount(bool Cond1, bool Cond2)() { // Assumes that the code is more complex, so you can't easily use something similar to: // count = cast(int)Cond1 + cast(int)Cond2; int count = 0; static if(Cond1) count++; static if(Cond2) count++; return count; } pragma(msg, myCount!(false, false)); // 0 pragma(msg, myCount!(true, false)); // 1 pragma(msg, myCount!(false, true)); // 1 pragma(msg, myCount!(true, true)); // 2 // e.g. store it in an enum enum Count = myCount!(true, false); // Or more simply... if possible for your use case enum Cond1 = true; enum Cond2 = false; enum Count = cast(int)Cond1 + cast(int)Cond2; // cast(int)true == 1; cast(int)false == 0 ``` Without additional context it's hard to give a concrete suggestion though, but I hope this helps.
Jan 28
On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:Suppose we have ``` enum Count = 0; static if (<Compile-time evaluable cond-1>) ... enum Count = Count + 1; // is disallowed else static if (<Compile-time evaluable cond-2>) ... enum Count = Count + 1; // is disallowed else ``` and the objective is to determine (at compile time) the number of times cond-1 or cond-2 is triggered (perhaps for setting size of a fixed array). So answer is zero, 1 or 2. But how to get that value into Count?I have a compile time counter gist, it isnt no where near that clean and it testing the line between a compiler bug
Jan 26