D - Looking for Syntax Help: Structs with no type names
- Russell Lewis (15/15) Sep 10 2002 I have some old C++ code I'm porting to D, in which I did a lot of stuff...
- Walter (12/27) Sep 11 2002 Write it like this:
- Sandor Hojtsy (14/43) Sep 11 2002 Semicolon missing here?
- Walter (5/16) Sep 12 2002 Correct. But when you want to declare a union of structs, this becomes a
- Russell Lewis (21/33) Sep 12 2002 Sure, I know that's possible. But that's no better than just defining
- Walter (3/18) Sep 12 2002 It becomes useful when doing structs of unions of structs etc.
- Russell Lewis (3/26) Sep 12 2002 True, forgot about that. But what about the rest of it? Any chance for...
- Walter (3/6) Sep 12 2002 I don't understand what the value of it is.
- Russ Lewis (13/19) Sep 13 2002 The "element1" and "valid.element1" values are tied to each other. I do...
- Walter (9/18) Sep 13 2002 don't
- Walter (8/8) Sep 13 2002 I forgot to mention. You can also do things like:
- Russ Lewis (7/15) Sep 13 2002 Right, that's what I do now, but it's ugly.
- Sandor Hojtsy (19/27) Sep 16 2002 In C++ you can do:
- Walter (6/8) Sep 18 2002 What's
- Russ Lewis (7/25) Sep 13 2002 Yeah, I do it like that for some other things, but don't really like it ...
- Sean L. Palmer (6/26) Sep 13 2002 Now that sounds cool.
- Sandor Hojtsy (11/40) Sep 16 2002 Indeed.
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.
Sep 10 2002
"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.
Sep 11 2002
"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...Semicolon missing here?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 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; };
Sep 11 2002
"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.
Sep 12 2002
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
Sep 12 2002
"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D80CF9E.5070201 deming-os.org...Walter wrote:It becomes useful when doing structs of unions of structs etc.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.
Sep 12 2002
Walter wrote:"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D80CF9E.5070201 deming-os.org...True, forgot about that. But what about the rest of it? Any chance for (or value in) the syntax sugar?Walter wrote:It becomes useful when doing structs of unions of structs etc.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.
Sep 12 2002
"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D80EAC8.5070109 deming-os.org...I don't understand what the value of it is.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?
Sep 12 2002
Walter wrote:"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D80EAC8.5070109 deming-os.org...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))) ]I don't understand what the value of it is.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?
Sep 13 2002
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D8201C9.B75E78DD deming-os.org...Walter wrote:don'tI don't understand what the value of it is.The "element1" and "valid.element1" values are tied to each other. Ireally want to clutter up the namespace with similar names. It'sparticularlyproblematic in my circumstance because the D code we're referring to is automatically generated from user definitions..so what if he defined bothan"element1" and "xelement1" value for his purposes? Then the user's "xelement1" conflicts with the automatically generated "xelement1", whichisjust a property of the user's "element1".Ok, I see. The way to do it is to create a unique prefix for your generated names, like _Foo_. Disallow the user from using _ in the names he types in, or disallow leading _.
Sep 13 2002
I forgot to mention. You can also do things like: struct Foo { struct Bar { int x; } Bar b; int x; } and refer to b.x.
Sep 13 2002
Walter wrote:I forgot to mention. You can also do things like: struct Foo { struct Bar { int x; } Bar b; int x; } and refer to b.x.Right, that's what I do now, but it's ugly. -- 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))) ]
Sep 13 2002
"Walter" <walter digitalmars.com> wrote in message news:alt3lr$235r$1 digitaldaemon.com...I forgot to mention. You can also do things like: struct Foo { struct Bar { int x; } Bar b; int x; } and refer to b.x.In C++ you can do: struct Foo { struct Bar { int x; }b; int x; }; (Walter: That's what I refered to as "named type", as compared to declaring an *anonymous* struct type inside an other struct.) and in C++ you can also do: struct Foo { struct { int x; }b; int x; }; I still don't understand the reason for disallowing these constructs. What's the problem with them? Too complex for humans? Sandor
Sep 16 2002
"Sandor Hojtsy" <hojtsy index.hu> wrote in message news:am46cp$1gjs$1 digitaldaemon.com...I still don't understand the reason for disallowing these constructs.What'sthe problem with them? Too complex for humans?The reason is to simplify the parsing. struct B { members } is not a type specifier in D.
Sep 18 2002
Walter wrote:"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D8201C9.B75E78DD deming-os.org...Yeah, I do it like that for some other things, but don't really like it :( -- 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))) ]Walter wrote:don'tI don't understand what the value of it is.The "element1" and "valid.element1" values are tied to each other. Ireally want to clutter up the namespace with similar names. It'sparticularlyproblematic in my circumstance because the D code we're referring to is automatically generated from user definitions..so what if he defined bothan"element1" and "xelement1" value for his purposes? Then the user's "xelement1" conflicts with the automatically generated "xelement1", whichisjust a property of the user's "element1".Ok, I see. The way to do it is to create a unique prefix for your generated names, like _Foo_. Disallow the user from using _ in the names he types in, or disallow leading _.
Sep 13 2002
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
Sep 13 2002
"Sean L. Palmer" <seanpalmer earthlink.net> wrote in message news:als29h$nng$1 digitaldaemon.com...Now that sounds cool.Indeed.Decorating member variables with little extra functions. It's be nice tobeable to decorate types that way too. Especially other classes.Well we already have properties for classes. The invention here is that they behave as having a value of a different (actually built-in) type. And that is *cast overloading*. If you have cast overloading, then - for example - you can declare a class whose instances can be directly assigned to and from integers, and still have your new properties, or boolean fields, or whatever. You can already do this in C++."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
Sep 16 2002