[Issue 5097] New: Safer unions with @tagged
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Oct 21 18:35:38 PDT 2010
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: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at 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: -------
More information about the Digitalmars-d-bugs
mailing list