www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - named union : needs 'this' to access member error

reply David L. Davis <SpottedTiger yahoo.com> writes:
# // needthis.d
# // WinXP SP2, D v0.127
# // BUG: "need 'this' to access member AsciiChar"
# // Seems like a named union error, because if it's 
# // unnamed it works just fine, but it's a MS struct
# // so I need it the way it is.
# private import std.c.windows.windows;
# 
# extern( Windows )
# {
# struct CHAR_INFO 
# {
#     union Char  //<-- if Char is commented out...ok
#     {
#         WCHAR UnicodeChar;
#         CHAR  AsciiChar;
#     }
#     
#     WORD Attributes;
# };
# }
# 
# int main()
# {
#     CHAR_INFO chiFill;
#     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
#                                                //   member AsciiChar
#     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
#     return 0;
# }

Output:
----------
C:\dmd>dmd needthis.d
needthis.d(26): need 'this' to access member AsciiChar

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 18 Jun 2005 02:24:55 +0000 (UTC), David L. Davis wrote:

 # // needthis.d
 # // WinXP SP2, D v0.127
 # // BUG: "need 'this' to access member AsciiChar"
 # // Seems like a named union error, because if it's 
 # // unnamed it works just fine, but it's a MS struct
 # // so I need it the way it is.
 # private import std.c.windows.windows;
 # 
 # extern( Windows )
 # {
 # struct CHAR_INFO 
 # {
 #     union Char  //<-- if Char is commented out...ok
 #     {
 #         WCHAR UnicodeChar;
 #         CHAR  AsciiChar;
 #     }
 #     
 #     WORD Attributes;
 # };
 # }
 # 
 # int main()
 # {
 #     CHAR_INFO chiFill;
 #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
 #                                                //   member AsciiChar
 #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
 #     return 0;
 # }
 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar
 
 C:\dmd>

If you name a union, then you must create an instance of it before you can reference its members. If you don't name a union, then the union becomes its own instance. struct CHAR_INFO { union { WCHAR UnicodeChar; CHAR AsciiChar; } } In this case, the anonymous union is also an instance of itself so you can reference its members directly. CHAR_INFO chiFill; chiFill.UnicodeChar = '\x20'; However ... struct CHAR_INFO { union Char { WCHAR UnicodeChar; CHAR AsciiChar; } Char aChar; } In this case, the union is named so you have to create an instance of it first, then reference member through that instance. CHAR_INFO chiFill; chiFill.aChar.UnicodeChar = '\x20'; If you use the union name itself in the reference, then you need to only reference 'static' members, and I'm not sure what that would be. -- Derek Melbourne, Australia 18/06/2005 12:46:21 PM
Jun 17 2005
next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Damn, Derek was faster ;)


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
Jun 17 2005
prev sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <trarz7rthih8.140fc8415dmrw$.dlg 40tude.net>, Derek Parnell says...
Not a bug.

If you name a union, then you must create an instance of it before you can
reference its members. If you don't name a union, then the union becomes
its own instance.


 struct CHAR_INFO 
 {
     union
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
 }

In this case, the anonymous union is also an instance of itself so you can
reference its members directly.

 CHAR_INFO chiFill;
 chiFill.UnicodeChar = '\x20';

However ...

 struct CHAR_INFO 
 {
     union Char
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
     Char aChar;
 }

In this case, the union is named so you have to create an instance of it
first, then reference member through that instance.

 CHAR_INFO chiFill;
 chiFill.aChar.UnicodeChar = '\x20';


If you use the union name itself in the reference, then you need to only
reference 'static' members, and I'm not sure what that would be.
  
-- 
Derek
Melbourne, Australia
18/06/2005 12:46:21 PM

Thanks Derek...funny thing tho, this example is a part of some D code that compiled fine with D v0.97 in July 2004 ( somehow I missed retesting this code some 30 version of D ago :P ). Thanks again. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
I believe it's intended to work this way... Make the union's name e.g. 
UChar and then declare it inside the struct, like:

# struct CHAR_INFO
# {
#     union UChar
#     {
#         WCHAR UnicodeChar;
#         CHAR  AsciiChar;
#     }
#
#     UChar Char;
#     WORD  Attributes;
# }



David L. Davis wrote:

 # // needthis.d
 # // WinXP SP2, D v0.127
 # // BUG: "need 'this' to access member AsciiChar"
 # // Seems like a named union error, because if it's 
 # // unnamed it works just fine, but it's a MS struct
 # // so I need it the way it is.
 # private import std.c.windows.windows;
 # 
 # extern( Windows )
 # {
 # struct CHAR_INFO 
 # {
 #     union Char  //<-- if Char is commented out...ok
 #     {
 #         WCHAR UnicodeChar;
 #         CHAR  AsciiChar;
 #     }
 #     
 #     WORD Attributes;
 # };
 # }
 # 
 # int main()
 # {
 #     CHAR_INFO chiFill;
 #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
 #                                                //   member AsciiChar
 #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
 #     return 0;
 # }
 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar

-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 17 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d902fe$2gfd$1 digitaldaemon.com>, Tom S says...
I believe it's intended to work this way... Make the union's name e.g. 
UChar and then declare it inside the struct, like:

# struct CHAR_INFO
# {
#     union UChar
#     {
#         WCHAR UnicodeChar;
#         CHAR  AsciiChar;
#     }
#
#     UChar Char;
#     WORD  Attributes;
# }



David L. Davis wrote:

 # // needthis.d
 # // WinXP SP2, D v0.127
 # // BUG: "need 'this' to access member AsciiChar"
 # // Seems like a named union error, because if it's 
 # // unnamed it works just fine, but it's a MS struct
 # // so I need it the way it is.
 # private import std.c.windows.windows;
 # 
 # extern( Windows )
 # {
 # struct CHAR_INFO 
 # {
 #     union Char  //<-- if Char is commented out...ok
 #     {
 #         WCHAR UnicodeChar;
 #         CHAR  AsciiChar;
 #     }
 #     
 #     WORD Attributes;
 # };
 # }
 # 
 # int main()
 # {
 #     CHAR_INFO chiFill;
 #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
 #                                                //   member AsciiChar
 #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
 #     return 0;
 # }
 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar

-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/

Thanks Tomasz! BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar name, who I remember made good levels / maps for "Heretic 2" back in the day. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
David L. Davis wrote:
 BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar
 name, who I remember made good levels / maps for "Heretic 2" back in the day.

Not me, sorry ;) I enjoyed playing Heretic2 but I never made a map for it -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 18 2005