Annotation of functions

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 21 15:59:55 UTC 2018


On 2/20/18 8:53 PM, psychoticRabbit wrote:
> On Tuesday, 20 February 2018 at 13:40:16 UTC, bauss wrote:
>>
>> I should probably have put an example usage to show how it's used:
>> ....
> 
> This makes we want to go back and program in C again ;-)
> 
> (but thanks for taking the time to demo/explain)

Mixins are one of those things that you need occasionally, but are weird 
to think about. But it's really akin to the C preprocessor (only 
better). They can be super-useful and make your life easier.

For example:

struct MyInt(T)
{
    T t;
    auto opBinary(string op)(const MyInt other) const
    {
       return mixin("MyInt(t " ~ op ~ " other.t)");
    }
}

void main()
{
    import std.stdio;
    alias mi = MyInt!int;
    mi i = mi(5);
    writeln(i + mi(6)); // MyInt!int(11)
    writeln(i - mi(2)); // MyInt!int(3)
    writeln(i * mi(4)); // MyInt!int(20)
    writeln(i / mi(2)); // MyInt!int(2)
    writeln(i ^ mi(1)); // MyInt!int(4)
    ... // etc.
}

The other thing that always gets me are all the different "is" 
expressions. I never remember those things.

-Steve


More information about the Digitalmars-d mailing list