Bug 3666 – Enhancement Request: Mixin Templates

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-02T13:35:00Z
Last change time
2015-06-09T05:13:45Z
Assigned to
nobody
Creator
simen.kjaras

Comments

Comment #0 by simen.kjaras — 2010-01-02T13:35:29Z
Currently, whenever you write a template to be mixed into another type, you specify at the other type how to use the template. Most templates that are made to be mixed in, will not be used in any other way. Hence, I propose that templates may marked 'mixin' at declaration point, and 'mixin' be deemed unnecessary at their point of use. 'mixin' should still be usable for unadorned templates, and should be a no-op for mixin templates. Proposed syntax: mixin template foo( T ) { T x; } struct bar { foo!( int ); }
Comment #1 by bus_dbugzilla — 2010-02-05T13:08:19Z
A two-part addendum: 1. As D's CTFE improves, there's been more and more reason to generate a string mixin using CTFE instead of a template. Simen's point that "Most templates that are made to be mixed in, will not be used in any other way" is also true for CTFE functions. So this enhancement request should also be extended to CTFE functions in addition to templates: ----------------------- mixin string foo2() { return "int a;"; } foo2(); a = 7; ----------------------- 2. It is probably worth noting that this enhancement request can be used to trivially re-implement the current string mixin: ----------------------- mixin string mixinString(string str) { return str; } mixinString("int a;"); -----------------------
Comment #2 by andrej.mitrovich — 2012-10-21T19:47:38Z
I don't know if D had mixins or template mixins back in 2010, these look like old ideas (?). Reopen if necessary.
Comment #3 by doob — 2012-10-22T00:03:07Z
These are still valid ideas and D did had templates back in 2010. Half of the proposal is already implemented, you can prefix a template declaration with "mixin". When a template is prefix with "mixin" you cannot use it like a regular template. But you still need the "mixin" keyword when mixing in the template.