D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20191 - Allow struct mixin declaration
Summary: Allow struct mixin declaration
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-04 14:42 UTC by RR
Modified: 2024-12-13 19:05 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 RR 2019-09-04 14:42:57 UTC
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.
Comment 1 dlangBugzillaToGithub 2024-12-13 19:05:28 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19619

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB