digitalmars.D.bugs - [Issue 10846] New: Allow defining functions in enum declarations
- d-bugmail puremagic.com (91/93) Aug 18 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10846
- d-bugmail puremagic.com (12/12) Aug 18 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10846
- d-bugmail puremagic.com (47/57) Aug 18 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10846
http://d.puremagic.com/issues/show_bug.cgi?id=10846 Summary: Allow defining functions in enum declarations Product: D Version: unspecified Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: andrej.mitrovich gmail.com 15:06:39 PDT --- Currently, thanks to UFCS, we can define most functions in module scope and make them act as if they were member functions. However this doesn't work nicely when you have multiple functions with the same name in different modules, for example: ----- module a; import b; struct A { } void test(A a) { } void main() { B b; b.test(); } ----- ----- module b; struct B { } void test(B b) { } ----- $ dmd -c a.da.d(9): Error: function a.test (A a) is not callable using argument types (B)This is D's protection against function hijacking. The diagnostic should improve, but that's beside the point. To work around this (without being forced to merge overloads with alias declarations), one can define these functions inside the structures, and there will be no errors: ----- module a; import b; struct A { void test() { } } void main() { B b; b.test(); } ----- ----- module b; struct B { void test() { } } ----- $ dmd -c a.dThis could also be considered a more "tightly" coupling. However, we cannot use this with enums because enums cannot have functions as members. So the following code becomes an error: ----- module a; import b; enum EA { a } void test(EA a) { } void main() { EB eb; eb.test(); } ----- ----- module b; enum EB { b } void test(EB eb) { } ----- $ dmd -c a.da.d(11): Error: function a.test (EA a) is not callable using argument types (EB)The way to work around this is to merge the overloads with alias declarations. But if functions inside of enums were allowed, then we could have a simpler workaround for the function hijacking protection. Unfortunately I can't think of a way to make the syntax look nice, for example: ----- enum E { a, b, c, void test() { } // looks awkward } ----- It looks weird to have a function embedded next to the members. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10846 15:09:20 PDT --- Another alternative is to relax the function hijacking protection a little bit. If function "foo" in module "a" takes a user-defined type, and function "foo" in module "b" takes another user-defined type, where the types are structures and have no subtyping relation to one another, then I think it's safe to allow these two functions to overload against one another without requiring the user to manually merge the overload set. Wouldn't this be safe? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10846 15:15:55 PDT ---Another alternative is to relax the function hijacking protection a little bit. If function "foo" in module "a" takes a user-defined type, and function "foo" in module "b" takes another user-defined type, where the types are structures and have no subtyping relation to one another, then I think it's safe to allow these two functions to overload against one another without requiring the user to manually merge the overload set. Wouldn't this be safe?Here's what I don't quite understand. This test-case fails: ----- module a; import b; struct A { } void test(A a) { } void main() { B b; b.test(); } ----- ----- module b; struct B { } void test(B b) { } ----- $ dmd -c a.da.d(10): Error: function a.test (A a) is not callable using argument types (B)But this test-case works: ----- module a; struct A { } void test(A a) { } ----- ----- module b; struct B { } void test(B b) { } ----- ----- module main; import a; import b; void main() { B b; b.test(); } ----- $ dmd -c main.dShouldn't then both test-cases either fail or succeed? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013