Skip to content
Metaprogramming library for the D language
D Dockerfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
source/quirks
.gitignore
Dockerfile
LICENSE
README.md
ci-azure-pipelines.yml
docs-azure-pipelines.yml
dub.json
dub.selections.json
trial.json

README.md

quirks

Build Status codecov

quirks is a small library to facilitate programming with traits and mixins.
See https://lljochemll.github.io/quirks/ for documentation and examples

Note: the API is very unstable right now as I'm experimenting with what I think is useful

Some features:

Quirks template

Swiss army knife for getting information about things

import quirks;
import std.stdio;

struct User {
    uint age;
    string name;

    void talk(string message) {
        import std.stdio : writeln;

        writeln(message);
    }
}

alias quirks = Quirks!User;

writeln(quirks.methods.length); // 1
writeln(quirks.fields.length); // 2
writeln(quirks.methods[0].name); // talk

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, hasField, hasMethod

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(hasField!(S, "id"));
static assert(!hasField!(S, "name"));
static assert(!hasField!(S, "doesNotExist"));

static assert(!hasMethod!(S, "id"));
static assert(hasMethod!(S, "name"));
static assert(!hasMethod!(S, "doesNotExist"));
static assert(hasMethod!(S, method => is(method.returnType == string)));

Parameters

import quirks;
import std.stdio;

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

alias parameters = Parameters!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");
    }
}
You can’t perform that action at this time.