GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Learn
  • Documentation
  • Language Reference
  • Library Reference
  • Command-line Reference
  • Feature Overview
  • Articles
  • Downloads
  • Packages
  • Community
  • Blog
  • Orgs using D
  • Twitter
  • Discord (community-run)

This view

  • Original page
  • New address

GeezerVison is showing dlang.org — String Mixins - D Programming Language

D Logo
Menu
  • Learn
  • Documentation
    • Language Reference
    • Library Reference
    • Command-line Reference
    • Feature Overview
    • Articles
  • Downloads
  • Packages
  • Community
    • Blog
    • Orgs using D
    • Twitter
    • Discord (community-run)
    • Forums
    • IRC
    • Wiki
    • Issues
    • Contributing
    • Contributors
    • Foundation
    • Security Team
    • Store
    • Donate
    • Sponsors
  • Resources
    • Tour
    • Books
    • Tutorials
    • Tools
    • Editors
    • IDEs
    • run.dlang.io
    • Visual D
    • Acknowledgments
    • D Style
    • Glossary
    • Sitemap
Search

Articles

overview

  • FAQ
  • const(FAQ)
  • Floating Point
  • Warnings
  • Rationale
  • Builtin Rationale
  • C to D
  • C++ to D
  • C Preprocessor vs D
  • Code coverage analysis
  • Exception Safety
  • Hijacking
  • Introduction to std.datetime
  • Lazy Evaluation
  • Migrating to Shared
  • String Mixins
  • Regular Expressions
  • SafeD
  • Templates Revisited
  • Template Constraints
  • Compile-time Sequences
  • Variadic Templates
  • Template Comparison
  • D Slices
  • D's Contract Programming
  • Writing Shared Libraries for Linux
  • Coralling Wild Pointers With ref return scope
  • Creating Windows DLLs
Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

String Mixins

String Mixins (not to be confused with template mixins) enable string constants to be compiled as regular D code and inserted into the program. Combining this with compile time manipulation of strings enables the creation of domain-specific languages.

For example, here we can create a template that generates a struct with the named members:

template GenStruct(string Name, string M1)
{
    const char[] GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}

mixin(GenStruct!("Foo", "bar"));

which generates:

struct Foo { int bar; }

Superficially, since D mixins can manipulate text and compile the result, it has some similar properties to the C preprocessor. But there are major, fundamental differences:

  • The C preprocessing step occurs before lexical analysis. This makes it impossible to lex or parse C without access to all of the context, including all #include'd files, paths and all relevant compiler switches. Mixins occur during semantic analysis, and do not affect the lexing or parsing process. Lexing and parsing can still occur without semantic analysis.
  • The C preprocessor can be used to create what appears to be different syntax:
    #define BEGIN {
    #define END   }
    
    BEGIN
      int x = 3;
      foo(x);
    END
    
    This monkey business is impossible with mixins. Mixed in text must form complete declarations, statements, or expressions.
  • C macros will affect everything following that has the same name, even if they are in nested scopes. C macros cut across all scopes. This problem is called being not "coding hygenic". Mixins follow the usual scoping rules, and is hygenic.
  • C preprocessing expressions follow a different syntax and have different semantic rules than the C language. The C preprocessor is technically a different language. Mixins are in the same language.
  • C const declarations and C++ templates are invisible to C preprocessing. Mixins can be manipulated using templates and const declarations.
Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Mon Nov 9 05:59:21 2026
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.