Re: How are extra copy constructor parameters used?

2021-12-29 Thread Rahul Sharma via Digitalmars-d-learn
On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote: The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax: https://dlang.org/spec/struct.html#struct-copy-constructor 1) I can't figure out how to use those extra

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Tejas via Digitalmars-d-learn
On Thursday, 30 December 2021 at 04:42:06 UTC, Tejas wrote: On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote: The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax:

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Tejas via Digitalmars-d-learn
On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote: The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax: https://dlang.org/spec/struct.html#struct-copy-constructor 1) I can't figure out how to use those extra

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 30 December 2021 at 02:04:30 UTC, Ali Çehreli wrote: On 12/29/21 5:14 PM, Paul Backus wrote: Therefore, when you write your own copy constructors, you should always use `inout` if possible, so that compiler-generated copy constructors will be able to copy instances of your

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Ali Çehreli via Digitalmars-d-learn
On 12/29/21 5:14 PM, Paul Backus wrote: Therefore, when you write your own copy constructors, you should always use `inout` if possible, so that compiler-generated copy constructors will be able to copy instances of your struct that appear as members of other structs. Excellent point. I

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote: 2) Unlike the examples there, I think the parameter should most usefully be defined as 'const' unless there is a special reason: struct S { // const(S) instead of S: this(ref const(S) that) { } } Do you agree? When the

How are extra copy constructor parameters used?

2021-12-29 Thread Ali Çehreli via Digitalmars-d-learn
The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax: https://dlang.org/spec/struct.html#struct-copy-constructor 1) I can't figure out how to use those extra parameters. For example, I can't find a special function name to

Re: AA and struct with const member

2021-12-29 Thread Era Scarecrow via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 01:11:13 UTC, Stanislav Blinov wrote: Because opIndexAssign cannot distinguish at compile time between initialization and assignment: ```d Stuff[Key] aa; aa[key] = Stuff(args); // ostensibly, initialization aa[key] = otherStuff; // assignment to existing

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: Mixin a function into a struct only if no member with that name already exists

2021-12-29 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 10:14:13 UTC, Tobias Pankrath wrote: How do I mixin a function only if it is not already present? This is the default behavior of template mixins.

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: Mixin a function into a struct only if no member with that name already exists

2021-12-29 Thread Tobias Pankrath via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 10:21:07 UTC, Stanislav Blinov wrote: On Wednesday, 29 December 2021 at 10:14:13 UTC, Tobias Pankrath wrote: How do I mixin a function only if it is not already present? Perhaps use opDispatch? That's a great idea and I'll look into it.

Re: Mixin a function into a struct only if no member with that name already exists

2021-12-29 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 10:14:13 UTC, Tobias Pankrath wrote: How do I mixin a function only if it is not already present? Perhaps use opDispatch?

Mixin a function into a struct only if no member with that name already exists

2021-12-29 Thread Tobias Pankrath via Digitalmars-d-learn
I am trying to implement the builder pattern for some structs. Currently I am auto implementing all methods by mixing them into the builder, but now I need some custom logic for some of the fields and I figured I just write them down by hand and mixin the rest. I tried checking if it is

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: opCast + dtor error

2021-12-29 Thread vit via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 01:00:53 UTC, Tejas wrote: On Tuesday, 28 December 2021 at 18:27:36 UTC, vit wrote: [...] Since a destructor ignores `const`, I think adding the `~this` to Foo manually is somehow making the compiler have to actually cast away const, which it is doing via