[Issue 7014] New: Better union initialization syntax
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Nov 25 17:02:48 PST 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7014
Summary: Better union initialization syntax
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 2011-11-25 17:01:51 PST ---
After using several unions for a while in D, I think the syntax to initialize
fields of unions is not handy enough. An idea from GNU-C:
import std.stdio;
union Foo { int i; double d; };
void main() {
int x;
double y;
Foo u = void;
auto u1 = cast(Foo)x;
writeln(typeof(u1).stringof);
auto u2 = cast(Foo)y; // Error: e2ir: cannot cast y of type double to type
Foo
writeln(typeof(u2).stringof);
}
See:
http://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html#Cast-to-Union
But I don't like that GNU-C idea, a problem with cast() is that it's not
precise enough, if there are two fields like this, what is a good way to assign
's' or 'u' using a cast()?
union Foo { int s; uint u; };
So I'd like a more explicit syntax.
A solution using named arguments seems good:
union Foo { int s; uint u; };
void bar(Foo f) {}
void main() {
bar(Foo(u:5));
}
--
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