digitalmars.D.learn - Better way to achieve the following
- JG (28/28) Jun 21 2022 Suppose we are often writing something like
- Steven Schveighoffer (4/5) Jun 21 2022 Use a pointer? Especially if you are using `.method` calls, this just
- JG (5/10) Jun 21 2022 Thanks for the suggestion. My immediate reaction is that for
- Steven Schveighoffer (8/19) Jun 21 2022 I use pointers for this purpose all the time. It's what they are for (to...
- Tejas (3/15) Jun 21 2022 Maybe check out `std.meta.Alias`?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/21) Jun 21 2022 An option is nested functions:
Suppose we are often writing something like ```d theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x; ``` One would like to something like ```d alias shortName = theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]; shortName = x; ``` but you can't alias an expression. You can do ```d (ref shortName) { shortName = x; }(theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]); ``` but that doesn't read well since the ``definition'' of shortName comes at the end. Another option is ```d auto aliasAs(alias f,T)(ref T x) { return f(x); } theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex].aliasAs! (ref shorName) { shortName = x; } ``` Thoughts?
Jun 21 2022
On 6/21/22 1:09 PM, JG wrote:Thoughts?Use a pointer? Especially if you are using `.method` calls, this just works seamlessly. -Steve
Jun 21 2022
On Tuesday, 21 June 2022 at 17:15:02 UTC, Steven Schveighoffer wrote:On 6/21/22 1:09 PM, JG wrote:Thanks for the suggestion. My immediate reaction is that for `.method` calls I would agree, but for assignments it is slightly less pleasant. Perhaps it is the best option.Thoughts?Use a pointer? Especially if you are using `.method` calls, this just works seamlessly. -Steve
Jun 21 2022
On 6/21/22 1:19 PM, JG wrote:On Tuesday, 21 June 2022 at 17:15:02 UTC, Steven Schveighoffer wrote:I use pointers for this purpose all the time. It's what they are for (to point at something). They even work in safe functions with dip1000 turned on. Yes it can get tedious to have to dereference in many cases. But at least if you forget and assign to `t` instead of `*t`, it probably doesn't compile. -SteveOn 6/21/22 1:09 PM, JG wrote:Thanks for the suggestion. My immediate reaction is that for `.method` calls I would agree, but for assignments it is slightly less pleasant. Perhaps it is the best option.Thoughts?Use a pointer? Especially if you are using `.method` calls, this just works seamlessly.
Jun 21 2022
On Tuesday, 21 June 2022 at 17:09:28 UTC, JG wrote:Suppose we are often writing something like ```d theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x; ``` One would like to something like ```d alias shortName = theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]; shortName = x; ``` but you can't alias an expression. [...]Maybe check out `std.meta.Alias`? https://dlang.org/phobos/std_meta.html#.Alias
Jun 21 2022
On 6/21/22 10:09, JG wrote:Suppose we are often writing something like ```d theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdNa e[theThirdIndex]=x; ``` One would like to something like ```d alias shortName = theFirstName[theFirstIndex].theSecondName[theSecondIndex].third ame[theThirdIndex]; shortName = x; ``` but you can't alias an expression.An option is nested functions: ref shortName() { return theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]; } shortName = x; Ali
Jun 21 2022