The OpenD Programming Language

bind

Passes the fields of a struct as arguments to a function.

Can be used with a function literal to give temporary names to the fields of a struct or tuple.

template bind(alias fun)
ref
bind
(
T
)
(
auto ref T args
)
if (
is(T == struct)
)

Members

Functions

bind
auto ref bind(T args)

Parameters

fun

Callable that the struct's fields will be passed to.

Return Value

A function that accepts a single struct as an argument and passes its fields to fun when called.

Examples

Giving names to tuple elements

import std.typecons : tuple;

auto name = tuple("John", "Doe");
string full = name.bind!((first, last) => first ~ " " ~ last);
assert(full == "John Doe");

Passing struct fields to a function

import std.algorithm.comparison : min, max;

struct Pair
{
    int a;
    int b;
}

auto p = Pair(123, 456);
assert(p.bind!min == 123); // min(p.a, p.b)
assert(p.bind!max == 456); // max(p.a, p.b)

In a range pipeline

import std.algorithm.iteration : map, filter;
import std.algorithm.comparison : equal;
import std.typecons : tuple;

auto ages = [
    tuple("Alice", 35),
    tuple("Bob",   64),
    tuple("Carol", 21),
    tuple("David", 39),
    tuple("Eve",   50)
];

auto overForty = ages
    .filter!(bind!((name, age) => age > 40))
    .map!(bind!((name, age) => name));

assert(overForty.equal(["Bob", "Eve"]));

Meta