Is there a way to make a function parameter accept only values that can be checked at compile time?
rempas
rempas at tutanota.com
Wed Dec 29 16:08:06 UTC 2021
On Wednesday, 29 December 2021 at 14:49:40 UTC, Steven
Schveighoffer wrote:
> On 12/29/21 3:55 AM, rempas wrote:
>
>> Thanks! That's cool but I don't want this to be this way. Or
>> at least I want it to be able to take a default value so we
>> don't have to get passed all the time.
> OK:
>
>>
>> ```
>> extern (C) void main() {
>> void print_num(int mul = 100)(int num) {
>> static if (is(mul == ten)) {
>> printf("%d\n", num * 10);
>> } else static if (is(mul == three)) {
>> printf("%d\n", num * 3);
>> } else {
>> printf("%d\n", num);
>> }
>> }
>>
>> int multi = 211;
>> print_num!3(10); // Set the value
>> print_num(30); // Get the default value, have the
>> "else" branch executed
>> }
>> ```
>
> Template parameters are just like regular parameters, but
> compile time.
>
> -Steve
Oh! Didn't knew you could do something like that! It works as
expected! Thanks a lot!
More information about the Digitalmars-d-learn
mailing list