http://d.puremagic.com/issues/show_bug.cgi?id=5097
Summary: Safer unions with @tagged Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@eml.cc 2010-10-21 18:34:53 PDT --- D language is designed to be safer than C and C++, yet it's not Java, it has to offer less safe features too, like unions. The failure of the Cyclone language shows that the safety features need to be light, minimal, not syntactically intrusive. The simpler way to make unions safe is to introduce an attribute as @tagged. When an union has a @tagged attribute, one word is added by the compiler to the union, that makes sure (with runtime tests) that the last written field is read. In release mode this invisible field and their tests vanish (a better feature is to test and enforce this at compile time, but this requires a more complex type system that D doesn't have). (If you want even more safety, unions with the @tagged attribute may not allow to take the address of fields, to avoid bypassing the runtime cheeks.) In the end the programmer that wants to use safer unions in D may just add the @tagged attribute, nothing else is required, and the union semantics is unchanged (just its size is changed in non-release mode). So this is a very light and easy to use safety net: @tagged union Foo { int i; float f; } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------