digitalmars.D.learn - Is there a way to make a function parameter accept only values that
- rempas (21/21) Dec 28 2021 I would like to know if that's possible. Actually I would like to
- Tobias Pankrath (2/23) Dec 28 2021 I think you have to make 'mul' a template parameter.
- Steven Schveighoffer (3/20) Dec 28 2021 -Steve
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/10) Dec 28 2021 Wasn't there a way of telling whether an 'auto ref' parameter is copied=...
- Stanislav Blinov (37/47) Dec 28 2021 Yup, different thing.
- rempas (5/39) Dec 29 2021 Thanks! That's awesome tho It will be annoying to have to type
- rempas (23/43) Dec 29 2021 Thanks! That's cool but I don't want this to be this way. Or at
- Steven Schveighoffer (4/25) Dec 29 2021 Template parameters are just like regular parameters, but compile time.
- rempas (4/31) Dec 29 2021 Oh! Didn't knew you could do something like that! It works as
- max haughton (3/24) Dec 28 2021 Why do you need this? What's wrong with a normal branch in this
- rempas (3/5) Dec 29 2021 Runtime performance. I want the value to get checked at compile
- max haughton (3/9) Dec 29 2021 If the value is known at compile time the compiler can pretty
- rempas (3/5) Dec 29 2021 How is this even possible?
- max haughton (4/9) Dec 29 2021 Inlining + constant propagation. Fancier iterations on those
- rempas (31/34) Dec 29 2021 Sounds like black magic? So If I write this:
- max haughton (13/47) Dec 29 2021 This is handled by the compiler backend. The simplest way it can
- rempas (6/18) Dec 29 2021 I didn't knew that compilers work like that. I thought that it
- Steven Schveighoffer (4/9) Dec 29 2021 Oof, just let the compiler do its job. Inlining and optimization will
I would like to know if that's possible. Actually I would like to do something like the following: ``` extern (C) void main() { void print_num(int num, comp_time_type int mul) { 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(10, 3); // Ok, accept this print_num(10, multi); // Error, do not accept this } ``` So I want to have "mul" only accept values that can be calculate at compile time so I can use it with things like "static if". Is this possible?
Dec 28 2021
On Tuesday, 28 December 2021 at 21:19:29 UTC, rempas wrote:I would like to know if that's possible. Actually I would like to do something like the following: ``` extern (C) void main() { void print_num(int num, comp_time_type int mul) { 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(10, 3); // Ok, accept this print_num(10, multi); // Error, do not accept this } ``` So I want to have "mul" only accept values that can be calculate at compile time so I can use it with things like "static if". Is this possible?I think you have to make 'mul' a template parameter.
Dec 28 2021
On 12/28/21 4:19 PM, rempas wrote: Here:``` extern (C) void main() { void print_num(int mul)(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); // Ok, accept this print_num!multi(10); // Error, do not accept this } ```-Steve
Dec 28 2021
On 12/28/21 2:06 PM, Steven Schveighoffer wrote:Wasn't there a way of telling whether an 'auto ref' parameter is copied=20 or not? void print_num()(int num, auto ref int mul) { // ? } And that would indicate that the argument was an rvalue? I realize that rvalues are not exactly what the OP is interested in. Ali=C2=A0=C2=A0 void print_num(int mul)(int num) {
Dec 28 2021
On Tuesday, 28 December 2021 at 22:30:30 UTC, Ali Çehreli wrote:On 12/28/21 2:06 PM, Steven Schveighoffer wrote:__traits(isRef, mul).Wasn't there a way of telling whether an 'auto ref' parameter is copied or not? void print_num()(int num, auto ref int mul) { // ? } And that would indicate that the argument was an rvalue?void print_num(int mul)(int num) {I realize that rvalues are not exactly what the OP is interested in.Yup, different thing. One can also do this kind of stuff: ```d import core.stdc.stdio; struct Literal(alias val) { enum value = val; } enum lit(alias val) = Literal!val.init; void print_num(Arg)(int num, Arg mul) { static if (is(Arg == Literal!val, alias val)) { static if (is(typeof(val) == string)) printf("mul by compile-time string \"%s\"!\n", val.ptr); else static if (is(typeof(val) == int) && (val == 3)) printf("mul by compile-time 3!\n"); else printf("mul by compile-time thing\n"); } else { printf("mul by runtime thing\n"); } } void main() { print_num(10, lit!"hello"); // mul by compile-time string "hello"! print_num(10, lit!3); // mul by compile-time 3! print_num(10, lit!'a'); // mul by compile-time thing print_num(10, 10); // mul by runtime thing } ```
Dec 28 2021
On Wednesday, 29 December 2021 at 01:34:22 UTC, Stanislav Blinov wrote:One can also do this kind of stuff: ```d import core.stdc.stdio; struct Literal(alias val) { enum value = val; } enum lit(alias val) = Literal!val.init; void print_num(Arg)(int num, Arg mul) { static if (is(Arg == Literal!val, alias val)) { static if (is(typeof(val) == string)) printf("mul by compile-time string \"%s\"!\n", val.ptr); else static if (is(typeof(val) == int) && (val == 3)) printf("mul by compile-time 3!\n"); else printf("mul by compile-time thing\n"); } else { printf("mul by runtime thing\n"); } } void main() { print_num(10, lit!"hello"); // mul by compile-time string "hello"! print_num(10, lit!3); // mul by compile-time 3! print_num(10, lit!'a'); // mul by compile-time thing print_num(10, 10); // mul by runtime thing } ```Thanks! That's awesome tho It will be annoying to have to type "lit!3" and not just pass it a literal or an "enum" or anything else that is guaranteed to be able to read at compile time.
Dec 29 2021
On Tuesday, 28 December 2021 at 22:06:50 UTC, Steven Schveighoffer wrote:On 12/28/21 4:19 PM, rempas wrote: Here: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. So something like this: ``` extern (C) void main() { void print_num(int num, comp_time_type int mul = 100) { 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(10, 3); // Set the value print_num(30); // Get the default value, have the "else" branch executed } ``` Is this possible?``` extern (C) void main() { void print_num(int mul)(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); // Ok, accept this print_num!multi(10); // Error, do not accept this } ```-Steve
Dec 29 2021
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
Dec 29 2021
On Wednesday, 29 December 2021 at 14:49:40 UTC, Steven Schveighoffer wrote:On 12/29/21 3:55 AM, rempas wrote:Oh! Didn't knew you could do something like that! It works as expected! Thanks a lot!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
Dec 29 2021
On Tuesday, 28 December 2021 at 21:19:29 UTC, rempas wrote:I would like to know if that's possible. Actually I would like to do something like the following: ``` extern (C) void main() { void print_num(int num, comp_time_type int mul) { 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(10, 3); // Ok, accept this print_num(10, multi); // Error, do not accept this } ``` So I want to have "mul" only accept values that can be calculate at compile time so I can use it with things like "static if". Is this possible?Why do you need this? What's wrong with a normal branch in this case.
Dec 28 2021
On Tuesday, 28 December 2021 at 22:26:33 UTC, max haughton wrote:Why do you need this? What's wrong with a normal branch in this case.Runtime performance. I want the value to get checked at compile time and use "static if" with it
Dec 29 2021
On Wednesday, 29 December 2021 at 08:56:47 UTC, rempas wrote:On Tuesday, 28 December 2021 at 22:26:33 UTC, max haughton wrote:If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky.Why do you need this? What's wrong with a normal branch in this case.Runtime performance. I want the value to get checked at compile time and use "static if" with it
Dec 29 2021
On Wednesday, 29 December 2021 at 11:09:04 UTC, max haughton wrote:If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky.How is this even possible?
Dec 29 2021
On Wednesday, 29 December 2021 at 15:53:38 UTC, rempas wrote:On Wednesday, 29 December 2021 at 11:09:04 UTC, max haughton wrote:Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky.How is this even possible?
Dec 29 2021
On Wednesday, 29 December 2021 at 16:27:22 UTC, max haughton wrote:Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.Sounds like black magic? So If I write this: ``` int add(int num, int num2) { return num1 + num2; } void main() { int number = add(10, 20); } ``` The parameters are literals so will D translate this to: ``` int add(int num, int num2) { return num1 + num2; } // Normal one int add_temp_func() { return 30; } // Created for the function call in main. No `add` instruction void main() { int number = add(10, 20); // Will actually create and call "add_temp_func" } ``` Or even better, this: ``` int add(int num, int num2) { return num1 + num2; } void main() { int number = add(10, 20); // What we will type and it will get replaced with the following line int number = 30; // So it calculates the result at compile times and doesn't even do a function call } ``` Is this what D can do? This is what I'm talking about when saying been able to use values at compile time.
Dec 29 2021
On Wednesday, 29 December 2021 at 16:51:47 UTC, rempas wrote:On Wednesday, 29 December 2021 at 16:27:22 UTC, max haughton wrote:This is handled by the compiler backend. The simplest way it can do this kind of optimization is by "inlining" the function. This is done by transplanting the function body into the place it's used. At this point the compiler simply sees "= 30 + 30" which it can trivially turn into "= 60" through something called constant-folding. The compiler can create new function bodies (like the temp one you introduce above) but this is a much more niche optimization. They favour inlining much more aggressively. I'm tempted to do a YouTube video of a D program being compiled all the way down the machine code, to show what the compiler does for you.Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.Sounds like black magic? So If I write this: ``` int add(int num, int num2) { return num1 + num2; } void main() { int number = add(10, 20); } ``` The parameters are literals so will D translate this to: ``` int add(int num, int num2) { return num1 + num2; } // Normal one int add_temp_func() { return 30; } // Created for the function call in main. No `add` instruction void main() { int number = add(10, 20); // Will actually create and call "add_temp_func" } ``` Or even better, this: ``` int add(int num, int num2) { return num1 + num2; } void main() { int number = add(10, 20); // What we will type and it will get replaced with the following line int number = 30; // So it calculates the result at compile times and doesn't even do a function call } ``` Is this what D can do? This is what I'm talking about when saying been able to use values at compile time.
Dec 29 2021
On Wednesday, 29 December 2021 at 17:20:59 UTC, max haughton wrote:This is handled by the compiler backend. The simplest way it can do this kind of optimization is by "inlining" the function. This is done by transplanting the function body into the place it's used. At this point the compiler simply sees "= 30 + 30" which it can trivially turn into "= 60" through something called constant-folding. The compiler can create new function bodies (like the temp one you introduce above) but this is a much more niche optimization. They favour inlining much more aggressively. I'm tempted to do a YouTube video of a D program being compiled all the way down the machine code, to show what the compiler does for you.I didn't knew that compilers work like that. I thought that it will treat the values normally like they were values that cannot get computed at compile time even if you pass them as literals. That's good to know
Dec 29 2021
On 12/29/21 3:56 AM, rempas wrote:On Tuesday, 28 December 2021 at 22:26:33 UTC, max haughton wrote:Oof, just let the compiler do its job. Inlining and optimization will take care of this for you (for the most part). -SteveWhy do you need this? What's wrong with a normal branch in this case.Runtime performance. I want the value to get checked at compile time and use "static if" with it
Dec 29 2021