Last update January 17, 2007

Mixins Vs Macros



Difference (last change) (no other diffs, normal page display)

Changed: 1c1,2
A notable posting (Walter Bright, Posting:digitalmars.D/1473) that is not yet reflected in the documentation:
Russ Lewis wrote:
"Walter (and everybody else), I was looking for your opinion on how mixins are different than macros. Haven't we just recreated the old moster in a new guise?"

Changed: 3c4
[[Posting]
It's an excellent question. Mixins are the same (and different from) templates just like C++ templates are the same and different from macros.

Deleted: 5,12d5
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message
news:c8do2v$1v33$1@digitaldaemon.com...
> Walter (and everybody else), I was looking for your opinion on how
> mixins are different than macros. Haven't we just recreated the old
> moster in a new guise?

It's an excellent question. Mixins are the same (and different from)
templates just like C++ templates are the same and different from macros.

Changed: 14,49c7,18

1) Mixins substitute in parsed declaration trees that pass muster with the
language syntax, macros substitute in arbitrary preprocessor tokens that
have no organization.

2) Mixins are in the same language. Macros are a *separate* and distinct
language layered on top of C++, with its own expression rules, its own
types, its distinct symbol table, its own scoping rules, etc.

3) Mixins are selected based on partial specialization rules, macros have no
overloading.

4) Mixins create a scope, macros do not.

5) Mixins are compatible with syntax parsing tools, macros are not.

6) Mixin semantic information and symbol tables are passed through to the
debugger, macros are lost in translation.

7) Mixins have override conflict resolution rules, macros just collide.

8) Mixins automatically create unique identifiers as required using a
standard algorithm, macros have to do it manually with kludgy token pasting.

9) Mixin value arguments with side effects are evaluated once, macro value
arguments get evaluated each time they are used in the expansion (leading to
weird bugs).

10) Mixin argument replacements don't need to be 'protected' with
parentheses to avoid operator precedence regrouping.

11) Mixins can be typed as normal D code of arbitrary length, multiline
macros have to be backslash line-spliced, can't use // to end of line
comments, etc.

12) Mixins can define other mixins. Macros cannot create other macros.
# Mixins substitute in parsed declaration trees that pass muster with the language syntax, macros substitute in arbitrary preprocessor tokens that have no organization.
# Mixins are in the same language. Macros are a separate and distinct language layered on top of C++, with its own expression rules, its own types, its distinct symbol table, its own scoping rules, etc.
# Mixins are selected based on partial specialization rules, macros have no overloading.
# Mixins create a scope, macros do not.
# Mixins are compatible with syntax parsing tools, macros are not.
# Mixin semantic information and symbol tables are passed through to the debugger, macros are lost in translation.
# Mixins have override conflict resolution rules, macros just collide.
# Mixins automatically create unique identifiers as required using a standard algorithm, macros have to do it manually with kludgy token pasting.
# Mixin value arguments with side effects are evaluated once, macro value arguments get evaluated each time they are used in the expansion (leading to weird bugs).
# Mixin argument replacements don't need to be 'protected' with parentheses to avoid operator precedence regrouping.
# Mixins can be typed as normal D code of arbitrary length, multiline macros have to be backslash line-spliced, can't use // to end of line comments, etc.
# Mixins can define other mixins. Macros cannot create other macros.

Changed: 53,54c22
]

Adapted from a post by WalterBright.

Russ Lewis wrote: "Walter (and everybody else), I was looking for your opinion on how mixins are different than macros. Haven't we just recreated the old moster in a new guise?"

It's an excellent question. Mixins are the same (and different from) templates just like C++ templates are the same and different from macros.

Some of the differences are:

  1. Mixins substitute in parsed declaration trees that pass muster with the language syntax, macros substitute in arbitrary preprocessor tokens that have no organization.
  2. Mixins are in the same language. Macros are a separate and distinct language layered on top of C++, with its own expression rules, its own types, its distinct symbol table, its own scoping rules, etc.
  3. Mixins are selected based on partial specialization rules, macros have no overloading.
  4. Mixins create a scope, macros do not.
  5. Mixins are compatible with syntax parsing tools, macros are not.
  6. Mixin semantic information and symbol tables are passed through to the debugger, macros are lost in translation.
  7. Mixins have override conflict resolution rules, macros just collide.
  8. Mixins automatically create unique identifiers as required using a standard algorithm, macros have to do it manually with kludgy token pasting.
  9. Mixin value arguments with side effects are evaluated once, macro value arguments get evaluated each time they are used in the expansion (leading to weird bugs).
  10. Mixin argument replacements don't need to be 'protected' with parentheses to avoid operator precedence regrouping.
  11. Mixins can be typed as normal D code of arbitrary length, multiline macros have to be backslash line-spliced, can't use // to end of line comments, etc.
  12. Mixins can define other mixins. Macros cannot create other macros.
I'm sure I'll think of more <g>.

Adapted from a post by WalterBright.


FolderDiscussions

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: January 17, 2007 22:47 (diff))