Allow binds to be provided by a function.

This is useful for situations where the initialisation of some object
should be deferred, eg. when there are distinct "setup" and "use" phases
to a tools lifecycle.
This commit is contained in:
Alec Thomas
2020-03-03 13:57:56 +11:00
parent c45ea59559
commit b8c82fea7c
6 changed files with 68 additions and 9 deletions
+2 -1
View File
@@ -91,7 +91,8 @@ func (c *Context) Bind(args ...interface{}) {
//
// BindTo(impl, (*MyInterface)(nil))
func (c *Context) BindTo(impl, iface interface{}) {
c.bindings[reflect.TypeOf(iface).Elem()] = reflect.ValueOf(impl)
valueOf := reflect.ValueOf(impl)
c.bindings[reflect.TypeOf(iface).Elem()] = func() (reflect.Value, error) { return valueOf, nil }
}
// Value returns the value for a particular path element.