kwargs

Strongly typed keyword arguments


Keywords
library
License
BSD-3-Clause
Install
dub fetch kwargs --version 0.0.2

Documentation

kwargs - Type-based keyword arguments for D

Build Status

Pass run or compile time parameters in any order to your functions

Runtime parameters

This library aims to make it easier to use type-safe APIs (the opposite of primitive obsession) without having to spell out all the values to be used, especially when many of them have defaults. For instance:

void build(BinName binName,
           CompileFlags cFlags = CompileFlags("-g", "-debug"),
           LinkerFlags lFlags = LinkerFlags(),
           ExtraStuff extraStuff = ExtraStuff());

If the user wants to use a non-default ExtraStuff value, they have to pass in CompilerFlags and LinkerFlags even though they might be happy with the defaults:

build(BinName(BinName("myapp"), CompilerFlags("-g", "debug"), LinkerFlags(), ExtraStuff(42)));

With this library, one can:

import kwargs;
alias awesomeBuild = kwargify!build;
// look ma, wrong order!
awesomeBuild(ExtraStuff(42), BinName("myapp"));

Compile-time parameters

Unfortunately, D currently lacks a way to reflect on template parameters, so the usage is slightly clumsier:

struct Foo { int val; }
struct Bar { int val; }
struct Baz { int val; }

int[] funImpl(Foo foo, Bar bar, Baz baz)() {
    return [foo.val, bar.val, baz.val];
}

alias fun = kwargify!(funImpl, Required!Foo, Optional!(Bar(2)), Optional!(Baz(3)));

fun!(Foo(1)).should == [1, 2, 3];
fun!(Baz(9), Foo(7)).should == [7, 2, 9];
fun!(Baz(9), Bar(8), Foo(7)).should == [7, 8, 9];

Hopefully the example is self-explanatory.