Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 10, 2002 Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
I have some old C++ code I'm porting to D, in which I did a lot of stuff like this: struct foo { struct { bool element1; bool element2; } valid; bar element1; bar element2; }; So you could access the members "valid.element1" and "valid.element2". This is no longer valid code, I have to name the "valid" struct and create an instance of the structure. Is there any more elegant way to do it? I always like the C++ way...creating a struct just to use one instance of it seems very hacky to me. |
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D7E352B.50303@deming-os.org... > I have some old C++ code I'm porting to D, in which I did a lot of stuff like this: > > struct foo { > struct { > bool element1; > bool element2; > } valid; > > bar element1; > bar element2; > }; > > So you could access the members "valid.element1" and "valid.element2". This is no longer valid code, I have to name the "valid" struct and create an instance of the structure. Is there any more elegant way to do it? I always like the C++ way...creating a struct just to use one instance of it seems very hacky to me. Write it like this: struct foo { struct { bool xelement1; bool xelement2; } bar element1; bar element2; }; In other words, you can use nested anonymous structs and unions. |
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:alpapt$2ifd$3@digitaldaemon.com... > > "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D7E352B.50303@deming-os.org... > > I have some old C++ code I'm porting to D, in which I did a lot of stuff like this: > > > > struct foo { > > struct { > > bool element1; > > bool element2; > > } valid; > > > > bar element1; > > bar element2; > > }; > > > > So you could access the members "valid.element1" and "valid.element2". This is no longer valid code, I have to name the "valid" struct and create an instance of the structure. Is there any more elegant way to do it? I always like the C++ way...creating a struct just to use one instance of it seems very hacky to me. > > Write it like this: > > struct foo { > struct { > bool xelement1; > bool xelement2; > } Semicolon missing here? > bar element1; > bar element2; > }; In this case I see no reason to do this. You can just put xelement1 and xelement2 right into struct foo. Can you define named types inside a struct? Such as: struct foo { struct valid_str { bool element1; bool element2; } valid; bar element1; bar element2; }; |
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> Write it like this:
>
> struct foo {
> struct {
> bool xelement1;
> bool xelement2;
> }
> bar element1;
> bar element2;
> };
>
> In other words, you can use nested anonymous structs and unions.
Sure, I know that's possible. But that's no better than just defining the x elements in the main struct. I would really prefer to access the elements as "valid.element1". (Actually, I'd like to make "valid" a boolean property of both the element1 and element2 variable...but perhaps that's asking too much.
Then again, you could conceivably use this sort of syntax:
struct foo {
bar element1
properties {
bool valid;
};
bar element2
properties {
bool valid;
};
}
The compiler would have to store the properties in the struct, but they would be accessible using the property syntax;
foo.element1 // refers to the variable, of type "bar"
foo.element1.valid // referes to the property
|
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandor Hojtsy | "Sandor Hojtsy" <hojtsy@index.hu> wrote in message news:alpdcb$2l8e$1@digitaldaemon.com... > In this case I see no reason to do this. You can just put xelement1 and xelement2 right into struct foo. Correct. But when you want to declare a union of structs, this becomes a useful layout tool. > Can you define named types inside a struct? Such as: > > struct foo { > struct valid_str { > bool element1; > bool element2; > } valid; > bar element1; > bar element2; > }; I don't understand what you mean by named type here. |
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D80CF9E.5070201@deming-os.org... > Walter wrote: > > Write it like this: > > > > struct foo { > > struct { > > bool xelement1; > > bool xelement2; > > } > > bar element1; > > bar element2; > > }; > > > > In other words, you can use nested anonymous structs and unions. > > Sure, I know that's possible. But that's no better than just defining the x elements in the main struct. It becomes useful when doing structs of unions of structs etc. |
September 12, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message
> news:3D80CF9E.5070201@deming-os.org...
>
>>Walter wrote:
>>
>>>Write it like this:
>>>
>>>struct foo {
>>> struct {
>>> bool xelement1;
>>> bool xelement2;
>>> }
>>> bar element1;
>>> bar element2;
>>>};
>>>
>>>In other words, you can use nested anonymous structs and unions.
>>
>>Sure, I know that's possible. But that's no better than just defining
>>the x elements in the main struct.
>
>
> It becomes useful when doing structs of unions of structs etc.
True, forgot about that. But what about the rest of it? Any chance for (or value in) the syntax sugar?
|
September 13, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D80EAC8.5070109@deming-os.org... > > It becomes useful when doing structs of unions of structs etc. > True, forgot about that. But what about the rest of it? Any chance for (or value in) the syntax sugar? I don't understand what the value of it is. |
September 13, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | Now that sounds cool. Decorating member variables with little extra functions. It's be nice to be able to decorate types that way too. Especially other classes. Sean "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D80CF9E.5070201@deming-os.org... > Sure, I know that's possible. But that's no better than just defining the x elements in the main struct. I would really prefer to access the elements as "valid.element1". (Actually, I'd like to make "valid" a boolean property of both the element1 and element2 variable...but perhaps that's asking too much. > > Then again, you could conceivably use this sort of syntax: > > struct foo { > bar element1 > properties { > bool valid; > }; > bar element2 > properties { > bool valid; > }; > } > > The compiler would have to store the properties in the struct, but they would be accessible using the property syntax; > > foo.element1 // refers to the variable, of type "bar" > foo.element1.valid // referes to the property |
September 13, 2002 Re: Looking for Syntax Help: Structs with no type names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D80EAC8.5070109@deming-os.org... > > > It becomes useful when doing structs of unions of structs etc. > > True, forgot about that. But what about the rest of it? Any chance for (or value in) the syntax sugar? > > I don't understand what the value of it is. The "element1" and "valid.element1" values are tied to each other. I don't really want to clutter up the namespace with similar names. It's particularly problematic in my circumstance because the D code we're referring to is automatically generated from user definitions..so what if he defined both an "element1" and "xelement1" value for his purposes? Then the user's "xelement1" conflicts with the automatically generated "xelement1", which is just a property of the user's "element1". -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
Copyright © 1999-2021 by the D Language Foundation