kwargs 0.0.2

Strongly typed keyword arguments

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

Manual usage
Put the following dependency into your project's dependences section:

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.

  • Atila Neves
0.0.2 2019-Feb-19
0.0.1 2019-Feb-11
~master 2019-Feb-20
Show all 3 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 7 downloads total

Short URL: