D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10846 - Allow defining functions in enum declarations
Summary: Allow defining functions in enum declarations
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on: 10848
Blocks:
  Show dependency treegraph
 
Reported: 2013-08-18 15:06 UTC by Andrej Mitrovic
Modified: 2022-07-06 05:15 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2013-08-18 15:06:39 UTC
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 Andrej Mitrovic 2013-08-18 15:09:20 UTC
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 Andrej Mitrovic 2013-08-18 15:15:55 UTC
(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 Andrej Mitrovic 2022-07-06 05:15:00 UTC
I think this was misguided and would pointlessly complicate the language. And these kinds of requests should now be DIP-ified.