LEVIATHAN vd52e79c4 · d52e79c486

Declarations

Dependency injection: bind and inject

Declare lexically scoped factory bindings that fill unbound parameters automatically, and select a binding explicitly with inject.

since 0.1.0-alpha.1linuxwindowswasm

Description

A bind declaration says how to build a value of a type wherever a call leaves a parameter of that type unfilled. It lets a function ask for a collaborator as an ordinary parameter without every caller having to pass it.

bind ILogger => ConsoleLogger();   // factory binding: an expression
bind ILogger { if (cfg) return A(); return B(); }   // factory binding: a body
greet();                            // injection is implicit when unambiguous
greet(inject ILogger);              // explicit selector

A binding is a factory: it runs each time a value is needed, so every injection gets a fresh value. Inside a function body both forms read that body's locals and its this, with the values they hold when the injection runs. A name in a factory means what it means at the bind: a local declared later, or one in a block or a lambda around the injection, does not shadow it. The => expr form and the block form follow the same rules as a method body. A binding is lexically scoped and the nearest binding of a type wins. Two bindings of the same type in one scope is a compile error. When a call leaves a parameter unfilled and has no default for it, the checker looks for a binding of the parameter's type, from the call site outward; inject Type as an argument names the binding explicitly.

Factory bindings and nearest-wins

use io::console;

interface ILogger { string log(string m); }
class ConsoleLogger : ILogger { string log(string m) => "console: " + m; }
class FileLogger : ILogger { string log(string m) => "file: " + m; }
void greet(ILogger logger, string who = "world") {
    console.writeln(logger.log("hello " + who));
}
bool useFile = true;
bind ILogger { if (useFile) { return FileLogger(); } return ConsoleLogger(); }
greet();
void inner() {
    bind ILogger => ConsoleLogger();
    greet();
    greet(who: "inner");
}
inner();
greet();
greet(inject ILogger);
file: hello world
console: hello world
console: hello inner
file: hello world
file: hello world

The file-level binding supplies the logger in greet(). Inside inner, a nearer binding shadows it.

Rules

  • bind T => expr; and bind T { ... } declare a factory binding for the type T. The body of the second form is a block that returns a T, with the same rules as a method body.
  • A binding is visible in the scope where it is declared, including nested scopes, and the nearest binding of a type wins.
  • A binding is also visible above its bind in the same block. An injection there reads the variables the bind will see, and it is a compile error if the factory reads a local that the block declares below the injection.
  • A => expr factory runs at the injection, as an operand of the call or expression there, and an assignment in it writes the variable its bind saw. An injection may be implicit, so that variable does not narrow while the factory is visible (lang.unions-and-optionals). A block factory is a lambda: it runs as one, so a write there to a variable its bind saw (an assignment, a mutating call or a collection verb) is a compile error, with the same diagnostic as in any lambda (cannot assign to captured 'n' in a lambda, see lang.primary-expressions); so is such a write in a lambda inside a => expr factory.
  • A factory's names are the ones visible at its bind. A file-level or namespace-level factory reads no local and no this of the injection site, and a function-body factory reads the variables its bind saw, whatever a nearer local of the same name at the site holds.
  • A binding is identified by the type it names, not by how the type is spelled: bind Lib::IRep, bind IRep after use Lib::IRep;, and bind R after use Lib::IRep as R; are one binding, and each fills a parameter or an inject that spells the type any of those ways.
  • Declaring two bindings of the same type in the same scope is a compile error, whatever the spellings.
  • A parameter is filled by injection only when it is not supplied by the call and has no default. See lang.argument-binding for how defaults and injection combine.
  • inject T written as an argument supplies the binding for T explicitly.
  • Place the binding around the call, not around the callee. A binding is resolved at the site where the unfilled parameter is bound, not along the call chain. A bind written inside a function body only reaches the calls made from that body. A binding that should wire a whole program must be in a scope that encloses every call it should fill.
  • The one injection the compiler makes for you is the contract reporter. bind IContractReporter => ...; (with use std::IContractReporter;) supplies the IContractReporter that is called with every ContractViolation raised by a call made in the scope of the binding (lang.contracts). A top-level binding in any file of the program is program-wide. With no binding in scope nothing is called; importing the name alone activates nothing.
  • The standard library declares capability interfaces IEnv, IConsole, IClock, IFileSystem and INet with system bindings. Writing use std::IConsole; brings the type in and activates its binding; see lang.bind-activation.

Examples

A binding must enclose the call it is meant to fill:

Bind placement

use io::console;

interface IEnv { string name(); }
class Real : IEnv { string name() => "real"; }
class Fake : IEnv { string name() => "fake"; }
void main(IEnv env) { console.writeln(env.name()); }
bind IEnv => Real();
void wrapper() {
    bind IEnv => Fake();
    main();
}
wrapper();
main();
void inner() {
    bind IEnv => Fake();
}
inner();
main();
fake
real
real

The Fake binding in wrapper encloses the call to main() there, so that call is filled with Fake. The binding inside inner encloses nothing that calls main, so the last call is unaffected.

Notes

Not implemented: aggregating bindings into a value (Bindings objects).

A bind without a factory, such as bind IFoo; or bind someObject;, is a compile error: 'bind' needs a factory: write 'bind T => expr;' or 'bind T { ... }'. To bind a ready-made object, write a factory that returns it, such as bind IConsole => recorder;, which returns the existing object each time it runs.

See also

  • Argument binding — How a call's arguments are matched to parameters, filled by defaults or injection, and how overloads are ranked.
  • Activating a namespace's bind with use — Importing a type with use also installs the binding its namespace exports for that type.
  • IConsole — What a program may say to the terminal.
  • IEnv — What a program may learn from the process environment: its arguments and its variables.
  • IClock — What a program may know about the time.
  • IFileSystem — What a program may do with the file system: open files and check whether a path exists.
  • INet — What a program may do with the network: open connections and listen for them.