pragma msg field name?
Chris Katko
ckatko at gmail.com
Tue Jun 27 05:28:37 UTC 2023
On Tuesday, 27 June 2023 at 04:56:19 UTC, Ali Çehreli wrote:
> On 6/26/23 21:25, Chris Katko wrote:
>
> > How do I get just the field name?
>
> I know .tupleof, which you can typeof() as well:
>
> class myObject{
> int field1, field2, field3;
>
> static foreach(field; typeof(this).tupleof)
> {
> pragma(msg, field.stringof);
> }
>
> static foreach(MemberType; typeof(typeof(this).tupleof)) {
> pragma(msg, MemberType);
> }
> }
>
> The output:
>
> field1
> field2
> field3
> int
> int
> int
>
> I had to consult what I wrote years ago:
>
> http://ddili.org/ders/d.en/tuples.html#ix_tuples..tupleof
>
> Ali
That seems to do the trick, I was really not expecting so much
text just to get something so simple!
At the moment I'm trying to take variables with an attribute
(rep) and then make a duplicate of them inside the struct. It
seems to work. If I had duplicate names, it fails. However, the
new fields don't appear to be showing up on a second enumeration:
enum rep;
struct multiplayerObject2
{
@rep ushort type;
@("rep2") ushort type2;
float x, y;
static foreach(t; typeof(this).tupleof)
{
// no if rep/rep2 here, i'm just testing:
pragma(msg, t.stringof); // does not see any new fields!
mixin("bool " ~ t.stringof ~ "25;"); // copy the fieldname
with a suffix
}
pragma(msg, "-----separator-----");
static foreach(t; typeof (this).tupleof) // again
{
pragma(msg, t.stringof); // does not see any new fields!
}
}
output
```
type
type2
x
y
-----separator-----
type
type2
x
y
```
However, if I do try to force the names to duplicate (say
"type2") I get an error involving some sort of __anonymous
subobject.
```
source/app.d-mixin-123(123,6): Error: variable
`app.multiplayerObject2.__anonymous.type2` conflicts with
variable `app.multiplayerObject2.type2` at source/app.d(116,19)
```
I also never realized you could put a static/static foreach
inside the body of a struct (and not a function) so I'm still
having trouble wrapping my head around that. Is it processing
top-down?
Jonathan M Davis: Yeah, it does what I listed if you add the UDA
to it.
More information about the Digitalmars-d-learn
mailing list