পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, January 30, 2017

D - Mixins

Mixins are structs that allows mixing the generated code into the source code. Mixins can be of the following types.
  • String Mixins
  • Template Mixins
  • Mixin name spaces

String Mixins

D has the capability to insert code as string as long as that string is known at compile time. The syntax of string mixins is shown below.
mixin (compile_time_generated_string)
A simple example for string mixins is shown below.
import std.stdio;

void main()
{
   mixin(`writeln("Hello World!");`);
}
When the above code is compiled and executed, it produces result something as follows:
Hello World!
Here is another example where we can pass the string in compile time so that mixins can use the functions to reuse code. It is shown below.
import std.stdio;

string print(string s)
{
   return `writeln("` ~ s ~ `");`;
}

void main()
{
   mixin (print("str1"));
   mixin (print("str2"));
}
When the above code is compiled and executed, it produces result something as follows:
str1
str2

Template Mixins

D templates define common code patterns, for the compiler to generate actual instances from that pattern. Templates can generate functions, structs, unions, classes, interfaces, and any other legal D code. The syntax of template mixins is shown below.
mixin a_template!(template_parameters)
A simple example for string mixins is shown below where we create a template with class Department and a mixin instantiating a template and hence making the the functions setName and printNames available to the structure college.
import std.stdio;

template Department(T, size_t count)
{
   T[count] names;

   void setName(size_t index, T name)
   {
      names[index] = name;
   }

   void printNames()
   {
      writeln("The names");

      foreach (i, name; names) 
      {
         writeln(i," : ", name);
      }
   }
}

struct College
{
   mixin Department!(string, 2);
}

void main()
{
   auto college = College();

   college.setName(0, "name1");
   college.setName(1, "name2");

   college.printNames();
}
When the above code is compiled and executed, it produces result something as follows:
The names
0 : name1
1 : name2

Mixin name spaces

Mixin name spaces are used to avoid ambiguities in template mixins. For example, there can be two variables, one defined explicitly in main and the other is mixed in. When a mixed-in name is the same as a name that is in the surrounding scope, then the name that is in the surrounding scope gets used. This example is shown below.
import std.stdio;

template Person()
{
   string name;
   void print()
   {
      writeln(name);
   }
}

void main()
{
   string name;

   mixin Person a;

   name = "name 1";
   writeln(name);

   a.name = "name 2";
   print();
}
When the above code is compiled and executed, it produces result something as follows:
name 1
name 2

No comments:

Post a Comment