Re: opIndexAssign

2023-10-05 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 5 October 2023 at 12:00:22 UTC, Timon Gehr wrote: void opIndexAssign(int value, int index){ i[index] = value; } In this case I need to define many operator overloads. For example (+=), this won't work without returns ref: ```d s[1] += 3;  assert(s.i == [2, 45]); //

Re: opIndexAssign

2023-10-05 Thread Timon Gehr via Digitalmars-d-learn
On 10/3/23 00:11, Salih Dincer wrote: Hi, opIndexAssign, which is void, cannot compromise with opIndex, which is a ref!  Solution: Using opSliceAssign.  Could this be a bug?  Because there is no problem in older versions (e.g. v2.0.83). ```d struct S {   int[] i;   ref opIndex(size_t

opIndexAssign

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
Hi, opIndexAssign, which is void, cannot compromise with opIndex, which is a ref! Solution: Using opSliceAssign. Could this be a bug? Because there is no problem in older versions (e.g. v2.0.83). ```d struct S {  int[] i;  ref opIndex(size_t index) => i[index];  auto opSliceAss

Re: Why is opIndexAssign replaced by opSlice here?

2021-10-18 Thread Elmar via Digitalmars-d-learn
On Monday, 18 October 2021 at 03:42:35 UTC, Paul Backus wrote: What happens here is, the compiler first tries the D2-style rewrite: ```d s.opIndexAssign(arr[1..4], s.opSlice!0(0, 3)) ``` However, that rewrite fails to compile, because your `opSlice` does not take a template argument specifyin

Re: Why is opIndexAssign replaced by opSlice here?

2021-10-17 Thread Imperatorn via Digitalmars-d-learn
On Monday, 18 October 2021 at 04:11:18 UTC, Paul Backus wrote: On Monday, 18 October 2021 at 03:42:35 UTC, Paul Backus wrote: My guess is that you got into this situation by trying to follow the example in the spec's section on ["Slice Assignment Operator Overloading"][2]. Unfortunately, that e

Re: Why is opIndexAssign replaced by opSlice here?

2021-10-17 Thread Paul Backus via Digitalmars-d-learn
On Monday, 18 October 2021 at 03:42:35 UTC, Paul Backus wrote: My guess is that you got into this situation by trying to follow the example in the spec's section on ["Slice Assignment Operator Overloading"][2]. Unfortunately, that example is incorrect. [2]: https://dlang.org/spec/operatorove

Re: Why is opIndexAssign replaced by opSlice here?

2021-10-17 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 17 October 2021 at 22:52:27 UTC, Elmar wrote: Hello Dear community. I'd like to overload `opIndexAssign` for a struct which wraps around a generic array (so that it can't support `opIndex` due to unknown return type). Broken down as much as possible this is the code:

Re: Why is opIndexAssign replaced by opSlice here?

2021-10-17 Thread Elmar via Digitalmars-d-learn
Btw, I should have written: `s.opIndexAssign(arr[1..4], s.opSlice(0,3));` But it compiles the same way.

Why is opIndexAssign replaced by opSlice here?

2021-10-17 Thread Elmar via Digitalmars-d-learn
Hello Dear community. I'd like to overload `opIndexAssign` for a struct which wraps around a generic array (so that it can't support `opIndex` due to unknown return type). Broken down as much as possible this is the code: ``` import std.stdio : writeln; import std.range : E

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-16 Thread james.p.leblanc via Digitalmars-d-learn
On Monday, 16 August 2021 at 10:48:19 UTC, Alexandru Ermicioi wrote: On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote: To be honest, I am not exactly sure what is happening here. I am unfamiliar with the "(T : T[])" syntax ... need to read That is template argument secializatio

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-16 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote: To be honest, I am not exactly sure what is happening here. I am unfamiliar with the "(T : T[])" syntax ... need to read That is template argument secialization. You're saying that T can be accept only types that are arrays of T

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-16 Thread Tejas via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:49:08 UTC, james.p.leblanc wrote: On Monday, 16 August 2021 at 06:42:48 UTC, Tejas wrote: If the code works, what's the problem? Hej Again, I was able to construct the working code shown above from help I obtained here in the forum and other resources. My ori

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:42:48 UTC, Tejas wrote: If the code works, what's the problem? Hej Again, I was able to construct the working code shown above from help I obtained here in the forum and other resources. My original code was not working ... but updated code is working fine ...

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote: On Monday, 16 August 2021 at 06:20:11 UTC, Tejas wrote: Maybe just write `T[]` in code rather than making it happen via the colon? I also have similar troubles, removing the default value always helped me. Can you show the c

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:20:11 UTC, Tejas wrote: Maybe just write `T[]` in code rather than making it happen via the colon? I also have similar troubles, removing the default value always helped me. Can you show the code where you're trying to apply this? Hej Tejas, Sure, here is

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Monday, 16 August 2021 at 06:12:42 UTC, james.p.leblanc wrote: On Monday, 16 August 2021 at 05:26:00 UTC, Tejas wrote: If you're finding the spec too hard, please try Ali's book. I'm sharing the part on operator overloading below: http://ddili.org/ders/d.en/operator_overloading.html Pleas

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Monday, 16 August 2021 at 05:26:00 UTC, Tejas wrote: If you're finding the spec too hard, please try Ali's book. I'm sharing the part on operator overloading below: http://ddili.org/ders/d.en/operator_overloading.html Please ping if you still have problems; I'll then write a full program.

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: Greetings, I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Sunday, 15 August 2021 at 21:28:53 UTC, james.p.leblanc wrote: On Sunday, 15 August 2021 at 21:15:02 UTC, Bastiaan Veelo wrote: On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc — Bastiaan. Bastiaan, Thanks once again, James On Sunday, 15 August 2021 at 21:28:53 UTC, james.p

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
the last couple of details regarding an array member? I attempted but the values always remain zero even when I set v=4. import std; struct A { int[5] a; int opIndexAssign(int v) // overloads a[] = v { writeln(__FUNCTION__); return 42

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread russhy via Digitalmars-d-learn
There is an example here: http://www.rosettacode.org/wiki/Multi-dimensional_array#D Look at the Matrix struct

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: struct A { int opIndexAssign(int v); // overloads a[] = v int opIndexAssign(int v, size_t[2] x); // overloads a[i .. j] = v int[2] opSlice(size_t x, size_t y); // overloads i .. j

Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
Greetings, I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v); // overloads a[] = v int opIndexAssign(int v, size_t[2

Re: What's opIndexAssign supposed to return ?

2020-02-25 Thread wjoe via Digitalmars-d-learn
On Tuesday, 25 February 2020 at 15:30:19 UTC, Ali Çehreli wrote: On 2/25/20 3:02 AM, wjoe wrote:> Lets say I've got 3 overloads of opIndexAssign: > > auto opIndexAssign(T t); > an internet search which didn't find any useful > information. I have examples for non-te

Re: What's opIndexAssign supposed to return ?

2020-02-25 Thread Ali Çehreli via Digitalmars-d-learn
On 2/25/20 3:02 AM, wjoe wrote:> Lets say I've got 3 overloads of opIndexAssign: > > auto opIndexAssign(T t); > an internet search which didn't find any useful > information. I have examples for non-templatized and templatized versions of opIndexAssign here: h

Re: What's opIndexAssign supposed to return ?

2020-02-25 Thread wjoe via Digitalmars-d-learn
On Tuesday, 25 February 2020 at 11:49:50 UTC, Petar Kirov [ZombineDev] wrote: On Tuesday, 25 February 2020 at 11:02:40 UTC, wjoe wrote: [...] opIndexAssign is the operator used in the following code: arr[1] = 8; It returns the element at index 1 (so 8 in this case) by reference. This

Re: What's opIndexAssign supposed to return ?

2020-02-25 Thread Petar via Digitalmars-d-learn
On Tuesday, 25 February 2020 at 11:02:40 UTC, wjoe wrote: Lets say I've got 3 overloads of opIndexAssign: auto opIndexAssign(T t); auto opIndexAssign(T t, size_t i); and auto opIndexAssign(T t, size_t[2] i); I would assume to return what I would return with opIndex but I'd rather no

What's opIndexAssign supposed to return ?

2020-02-25 Thread wjoe via Digitalmars-d-learn
Lets say I've got 3 overloads of opIndexAssign: auto opIndexAssign(T t); auto opIndexAssign(T t, size_t i); and auto opIndexAssign(T t, size_t[2] i); I would assume to return what I would return with opIndex but I'd rather not act upon assumptions. But if yes is it supposed to be

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread SrMordred via Digitalmars-d-learn
void main() { auto s = S(); s["b", "c"] = s["a"]; } Prints a ["b", "c"] Ali I thought about this too, thanks!

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread Ali Çehreli via Digitalmars-d-learn
opIndexAssign(int value, string[] indexes...) { writeln(indexes); } } void main() { auto s = S(); s["b", "c"] = s["a"]; } Prints a ["b", "c"] Ali

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread SrMordred via Digitalmars-d-learn
On Monday, 18 September 2017 at 15:14:20 UTC, Moritz Maxeiner wrote: On Monday, 18 September 2017 at 15:11:34 UTC, Moritz Maxeiner wrote: gets rewritten to --- t.opIndex("b").opIndexAssign(t["a"].value, "c"); --- Sorry, forgot one level of rewriting: --

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread Moritz Maxeiner via Digitalmars-d-learn
On Monday, 18 September 2017 at 15:11:34 UTC, Moritz Maxeiner wrote: gets rewritten to --- t.opIndex("b").opIndexAssign(t["a"].value, "c"); --- Sorry, forgot one level of rewriting: --- t.opIndex("b").opIndexAssign(t.opIndex("a").value, "c"); ---

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread Moritz Maxeiner via Digitalmars-d-learn
On Sunday, 17 September 2017 at 18:52:39 UTC, SrMordred wrote: struct Test{ [...] } Test t; As described in the spec [1] t["a"] = 100; gets rewritten to --- t.opIndexAssign(100, "a"); --- , while t["b"]["c"] = t["a"].value; gets r

Re: OpIndex/OpIndexAssign strange order of execution

2017-09-18 Thread SrMordred via Digitalmars-d-learn
Should I report this as a bug? I tried a C++ equivalent code and it execute in the expected order.

OpIndex/OpIndexAssign strange order of execution

2017-09-17 Thread SrMordred via Digitalmars-d-learn
struct Test{ @property int value(){ writeln("property value : ", _value); return _value; } int _value; Test opIndex( string index ) { writeln( "opIndex : index : ", index ); return this; } Test opIndexAssign(int

Re: lvalue - opIndexAssign - Tango

2009-03-15 Thread bearophile
The Anh Tran: > No offense, but for a beginner like me, those mixin are similar to C > macro hacks years ago. :) They share part of the same purposes. But D string mixing are scoped, while C macros cut across scopes. This makes mixins quite safer to use. Bye, bearophile

Re: lvalue - opIndexAssign - Tango

2009-03-15 Thread The Anh Tran
downs wrote: Take a look at tools.behave_as (for phobos). It only works properly with built-in types though. http://dsource.org/projects/scrapple/browser/trunk/tools/tools/behave_as.d It should let you implement a HashMap that supports += and similar, at least for built-ins. Many thanks. No of

Re: lvalue - opIndexAssign - Tango

2009-03-15 Thread downs
345; // error not lvalue > hm[123]++;// error > > D document says current opIndexAssign does not work as lvalue. But why > can builtin AA can that? How can i copy builtin AA behaviour? > Take a look at tools.behave_as (for phobos). It only works properly with built-in typ

Re: lvalue - opIndexAssign - Tango

2009-03-14 Thread The Anh Tran
Daniel Keep wrote: You could try the Tango IRC channel: That, or the Tango forums: http://dsource.org/projects/tango/forums You can report problems with Tango via the ticket system: http://dsource.org/projects/tango/report ("New Ticket" is down the bottom of the page.) I tried to register. Bu

Re: lvalue - opIndexAssign - Tango

2009-03-13 Thread Denis Koroskin
] += 12345; // error not lvalue hm[123]++; // error D document says current opIndexAssign does not work as lvalue. But why can builtin AA can that? How can i copy builtin AA behaviour? -- Forgive my noob, where is the place to ask question, report bug for Tango? 1. I

Re: lvalue - opIndexAssign - Tango

2009-03-13 Thread Daniel Keep
23] += 12345; // error not lvalue > hm[123]++;// error > > D document says current opIndexAssign does not work as lvalue. But why > can builtin AA can that? How can i copy builtin AA behaviour? You can't. This is a hole in the language at the moment, hopefully solved by the intr

lvalue - opIndexAssign - Tango

2009-03-13 Thread The Anh Tran
]++; // error D document says current opIndexAssign does not work as lvalue. But why can builtin AA can that? How can i copy builtin AA behaviour? -- Forgive my noob, where is the place to ask question, report bug for Tango? 1. I can't compile D code using tango hashmap in debug