Bug 10846 – Allow defining functions in enum declarations

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-18T15:06:39Z
Last change time
2022-07-06T05:15:00Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Depends on
10848

Comments

Comment #0 by andrej.mitrovich — 2013-08-18T15:06:39Z
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.d > a.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.d > This 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.d > a.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.
Comment #1 by andrej.mitrovich — 2013-08-18T15:09:20Z
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?
Comment #2 by andrej.mitrovich — 2013-08-18T15:15:55Z
(In reply to comment #1) > 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.d > a.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.d > Shouldn't then both test-cases either fail or succeed?
Comment #3 by andrej.mitrovich — 2022-07-06T05:15:00Z
I think this was misguided and would pointlessly complicate the language. And these kinds of requests should now be DIP-ified.