kwargs 0.0.2

Strongly typed keyword arguments


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

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.

Authors:
  • Atila Neves
Dependencies:
none
Versions:
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

  • 4 downloads this week

  • 4 downloads this month

  • 4 downloads total

Score:
0.5
Short URL:
kwargs.dub.pm