www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10846] New: Allow defining functions in enum declarations

reply d-bugmail puremagic.com writes:
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


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-08-18
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.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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10846



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-08-18
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
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10846



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-08-18
15:15:55 PDT ---
(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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 18 2013