Re: copy must be const?!?

2024-07-30 Thread swigy food via Digitalmars-d-learn
Hello A const copy ensures the copied value remains unchanged, providing safety and predictability. If the original is const, copying it as non-const could introduce unintended side effects. To modify a copied value, create a mutable copy explicitly. For file systems, copying write-protected fi

Re: copy must be const?!?

2024-07-29 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 26 July 2024 at 10:04:45 UTC, Jonathan M Davis wrote: On Friday, July 26, 2024 2:17:21 AM MDT Dom DiSc via Digitalmars-d-learn wrote: If you are not able to construct a mutable copy of a type, why on earth are you handing it over by value?!? Why not? Because you can't make a mutab

Re: copy must be const?!?

2024-07-26 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, July 26, 2024 2:17:21 AM MDT Dom DiSc via Digitalmars-d-learn wrote: > On Thursday, 25 July 2024 at 13:07:03 UTC, Jonathan M Davis wrote: > > On Thursday, July 25, 2024 6:00:58 AM MDT Dom DiSc via > > > >> But a parameter given by value is ALWAYS a copy. > > > > It has to be a _full_, i

Re: copy must be const?!?

2024-07-26 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 25 July 2024 at 13:07:03 UTC, Jonathan M Davis wrote: On Thursday, July 25, 2024 6:00:58 AM MDT Dom DiSc via But a parameter given by value is ALWAYS a copy. It has to be a _full_, independent copy. If you're talking about integer types, that's a non-issue, but if you're talking

Re: copy must be const?!?

2024-07-26 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 26 July 2024 at 02:34:12 UTC, Andy Valencia wrote: On Thursday, 25 July 2024 at 13:07:03 UTC, Jonathan M Davis wrote: It's most definitely not a bug that IFTI (Implicit Function Template Instantiation) instantiates the template with the exact type that it's given. The "principle

Re: copy must be const?!?

2024-07-25 Thread Quirin Schroll via Digitalmars-d-learn
On Thursday, 25 July 2024 at 13:07:03 UTC, Jonathan M Davis wrote: On Thursday, July 25, 2024 6:00:58 AM MDT Dom DiSc via Digitalmars-d-learn wrote: > And no, in general, you don't want to be casting away const > or immutable. There are cases where it can work (e.g. if the > cast does a copy, w

Re: copy must be const?!?

2024-07-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 25, 2024 6:00:58 AM MDT Dom DiSc via Digitalmars-d-learn wrote: > > And no, in general, you don't want to be casting away const or > > immutable. There are cases where it can work (e.g. if the cast > > does a copy, which it would with an integer type) > > But a parameter given by

Re: copy must be const?!?

2024-07-25 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 25 July 2024 at 08:42:29 UTC, Jonathan M Davis wrote: It's not a cast. Casts in D use the keyword, cast - e.g. return --(cast(T)x); Rather, Dennis' solution is constructing a value of the given type. For it to work, T must be constructible from an immutable T - which works wi

Re: copy must be const?!?

2024-07-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 25, 2024 12:50:04 AM MDT Dom DiSc via Digitalmars-d-learn wrote: > On Wednesday, 24 July 2024 at 15:40:28 UTC, Dennis wrote: > >> Is there a way to tell the compiler that it should discard > >> "const" and "immutable" if it needs to create a copy? > >> Unqual!T doesn't work :-( >

Re: copy must be const?!?

2024-07-24 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 24 July 2024 at 15:40:28 UTC, Dennis wrote: Is there a way to tell the compiler that it should discard "const" and "immutable" if it needs to create a copy? Unqual!T doesn't work :-( When you add `const` or `immutable` before the template type parameter, it will infer T as simpl

copy must be const?!?

2024-07-24 Thread Dom DiSc via Digitalmars-d-learn
In following code: ```d int test(int x) { return --x; } T test2(T)(T x) { return --x; } void main() { const int v = 3; writeln(test(v)); writeln(test2(v)); // doesn't compile } ``` test2 (just like test) works on a copy of x. Why is this copy const?!? If I copy a const or immutable obj