Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread max haughton via Digitalmars-d-learn
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: 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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread max haughton via Digitalmars-d-learn
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: 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? Inlining + constant

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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)

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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?

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/29/21 3:56 AM, rempas wrote: 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 Oof, just let the

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread Steven Schveighoffer via Digitalmars-d-learn
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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread max haughton via Digitalmars-d-learn
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: 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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
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

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 22:06:50 UTC, Steven Schveighoffer wrote: 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)) {  

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 22:30:30 UTC, Ali Çehreli wrote: On 12/28/21 2:06 PM, Steven Schveighoffer wrote:    void print_num(int mul)(int num) { 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) { // ? }

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread Ali Çehreli via Digitalmars-d-learn
On 12/28/21 2:06 PM, Steven Schveighoffer wrote:    void print_num(int mul)(int num) { 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? I

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread max haughton via Digitalmars-d-learn
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",

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread Steven Schveighoffer via Digitalmars-d-learn
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);   

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread Tobias Pankrath via Digitalmars-d-learn
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",

Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread rempas via Digitalmars-d-learn
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)) {