What are string mixins in D?

What are mixins in D?

Mixins is a feature of D that enables it to mix generated code into source code. Mixins in D are of three types:

  1. String mixins
  2. Template mixins
  3. Mixins namespaces

In this shot we'll look at string mixins.

What are string mixins?

String mixins are a type of mixin where code is compiled and executed even if it's in the string format. These strings have to be known at compile-time. Let's look at an example.

Syntax

mixin (compile_time_generated_string)
Mixins syntax

Example

import std.stdio;
void main() {
mixin(`writeln("Educative Learning");`);
}

Explanation

Line 4: We call the mixin() method. Notice how we use backticks to wrap the writeln() method. We do this so that D can recognize that we want to execute a string mixin. It then automatically looks for the executable part of the string.

Free Resources