digitalmars.D.learn - "macro" expansion to build switch case code
- Paul (16/16) Jul 02 2023 I have a struct similar to the following example. I'd like to
- nbdusr (2/18) Jul 02 2023 https://forum.dlang.org/post/rqgvruwgrldklarablii@forum.dlang.org
- kdevel (40/56) Jul 02 2023 Thanks for your interesting question!
- Steven Schveighoffer (15/31) Jul 02 2023 Use a static foreach:
- Paul (3/19) Jul 03 2023 Perfect. Thanks Steve!
I have a struct similar to the following example. I'd like to
build an adder method without having to code the whole method.
How do I use the D language to do this? Template, mixins,
CTFE..all of them?
```d
struct myS {
int a, b, c, d, e, f, g, h, i;
adder(string s, int n) {
final switch (s) {
case "aa" :
a += n; break;
case "bb" :
b += n; break;
...
```
Thanks for any assistance.
Jul 02 2023
On Sunday, 2 July 2023 at 17:02:44 UTC, Paul wrote:
I have a struct similar to the following example. I'd like to
build an adder method without having to code the whole method.
How do I use the D language to do this? Template, mixins,
CTFE..all of them?
```d
struct myS {
int a, b, c, d, e, f, g, h, i;
adder(string s, int n) {
final switch (s) {
case "aa" :
a += n; break;
case "bb" :
b += n; break;
...
```
Thanks for any assistance.
https://forum.dlang.org/post/rqgvruwgrldklarablii forum.dlang.org
Jul 02 2023
On Sunday, 2 July 2023 at 17:02:44 UTC, Paul wrote:
I have a struct similar to the following example. I'd like to
build an adder method without having to code the whole method.
How do I use the D language to do this? Template, mixins,
CTFE..all of them?
```d
struct myS {
int a, b, c, d, e, f, g, h, i;
adder(string s, int n) {
final switch (s) {
case "aa" :
a += n; break;
case "bb" :
b += n; break;
...
```
Thanks for any assistance.
Thanks for your interesting question!
```
module mys;
import std.exception;
class UnknownRegister : Exception {
mixin basicExceptionCtors;
}
enum string [] DefaultRegisterSet = [
"a", "b", "c", "d", "e", "f", "g", "h", "i"
];
struct myS {
int [string] registers;
this (string [] register_names)
{
foreach (r; register_names)
registers [r] = 0;
}
void adder (string s, int n)
{
auto p = s in registers;
enforce!UnknownRegister (p);
*p += n;
}
int opDispatch (string s) ()
{
auto p = s in registers;
enforce!UnknownRegister (p);
return *p;
}
}
unittest {
auto m = myS (DefaultRegisterSet);
m.adder ("a", 7);
m.adder ("a", 8);
assert (m.a == 15);
assertThrown!UnknownRegister (m.adder ("j", -1));
assertThrown!UnknownRegister (m.j == 0);
}
```
Jul 02 2023
On 7/2/23 1:02 PM, Paul wrote:
I have a struct similar to the following example. I'd like to build an
adder method without having to code the whole method. How do I use the D
language to do this? Template, mixins, CTFE..all of them?
```d
struct myS {
int a, b, c, d, e, f, g, h, i;
adder(string s, int n) {
final switch (s) {
case "aa" :
a += n; break;
case "bb" :
b += n; break;
...
```
Thanks for any assistance.
Use a static foreach:
```d
import std.traits; // for FieldNameTuple. there are other ways, but this
is the most straightforward
switchlabel: // this is needed for break inside a static foreach
final switch(s) {
static foreach(mem; FieldNameTuple!myS) {
case mem ~ mem:
__traits(getMember, this, mem) += n;
break switchalbel;
}
}
```
-Steve
Jul 02 2023
On Sunday, 2 July 2023 at 20:27:47 UTC, Steven Schveighoffer wrote:On 7/2/23 1:02 PM, Paul wrote:Perfect. Thanks Steve![...]Use a static foreach: ```d import std.traits; // for FieldNameTuple. there are other ways, but this is the most straightforward switchlabel: // this is needed for break inside a static foreach final switch(s) { static foreach(mem; FieldNameTuple!myS) { case mem ~ mem: __traits(getMember, this, mem) += n; break switchalbel; } } ``` -Steve
Jul 03 2023









nbdusr <. example.com> 