quirks 0.0.1

Metaprogramming library for the D language


To use this package, run the following command in your project's root directory:

quirks

Quirks is a small library to facilitate programming with traits and mixins.

Some features:

interpolateMixin

Adds string interpolation to mixins

import quirks;
import std.stdio;

class Foo {
    int id = 1;
    uint age = 23;
    string name = "foo";
}

auto foo = new Foo;

// version with interpolateMixin
static foreach(member; FieldNameTuple!(Foo)) {
    mixin(interpolateMixin(q{
        writeln("Field ${member} has a value of: ", foo.${member});
    }));
}

// version without
static foreach(member; FieldNameTuple!(Foo)) {
    mixin(`
        writeln("Field ` ~ member ~ ` has a value of: ", foo.` ~ member ~ `);
    `);
}

hasMember, hasMethod, hasField

import quirks;

struct Foo {
    long id;
    string name() {
        return "name";
    }
}

static assert(hasMember!(S, "id"));
static assert(hasMember!(S, "name"));
static assert(!hasMember!(S, "doesNotExist"));
static assert(hasMember!(S, (member => member == "id")));

static assert(!hasMethod!(S, "id"));
static assert(hasMethod!(S, "name"));
static assert(!hasMethod!(S, "doesNotExist"));

static assert(hasField!(S, "id"));
static assert(!hasField!(S, "name"));
static assert(!hasField!(S, "doesNotExist"));

getParameters, getParameterDefaultValues, getParameterNames, getParameterTypes, getReturnType

import quirks;
import std.stdio;

uint calculateAge(long birthYear, string planet = "earth");

alias parameters = getParameters!calculateAge;

static foreach (parameter; parameters) {
    write("Parameter " , parameter.name, " has a type of ", parameter.type.stringof);

    static if (parameter.hasDefaultValue) {
        writeln(" and a default value of ", parameter.defaultValue);
    } else {
        writeln(" and no default value");
    }
}
Authors:
  • lljochemll
Dependencies:
none
Versions:
0.1.2 2019-Aug-13
0.1.1 2019-Aug-10
0.1.0 2019-Jul-21
0.0.1 2019-Jun-25
~master 2019-Aug-13
Show all 7 versions
Download Stats:
  • 0 downloads today

  • 3 downloads this week

  • 6 downloads this month

  • 7 downloads total

Score:
0.4
Short URL:
quirks.dub.pm