Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow struct mixin declaration #19619

Open
dlangBugzillaToGithub opened this issue Sep 4, 2019 · 0 comments
Open

Allow struct mixin declaration #19619

dlangBugzillaToGithub opened this issue Sep 4, 2019 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

Radu Racariu (@rracariu) reported this on 2019-09-04T14:42:57Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=20191

Description

Compiler should allow the following syntax:

```
struct Base
{
  int foo;
}

struct Derived mixin Base
{
    int mult(int bar)
    {
        return foo * bar;
    }
}

void main()
{
  auto d = Derived(10);
  d.mult(2);
}
```

The syntax allows to mixin any other struct, template mixin or interface in the body of the type that is declared. Syntax should allow mixin in parameterized types also, i.e.
 
`struct Foo(T) mixin Bar!T`

The use case for this is to simplify creation of POD types that use common functionality, akin to inheritance - but not polymorphic.

Another useful feature will be the possibility to implement interface contracts non-polymorphically, for example:

```
interface Animal
{
  void talk();
  
  final void terminate()
  {
    // implementation provided
  }
}

struct Derived mixin Animal
{
    void talk()
    {
        // implement
    }
}

void main()
{
  Derived d;
  d.talk();
  d.terminate();
}
```

The compiler will emit an error for methods not implemented from mixin interfaces

The usage of the `mixin` keyword instead of the `:` symbol for existing class inheritance signals that the two are not equivalent, and it also builds on the existing semantics of the mixin concept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant