Skip to content

Add a generic BindFor that can usually replace BindTo#590

Merged
alecthomas merged 1 commit intoalecthomas:masterfrom
jwreschnig-utiq:bindfor
Mar 31, 2026
Merged

Add a generic BindFor that can usually replace BindTo#590
alecthomas merged 1 commit intoalecthomas:masterfrom
jwreschnig-utiq:bindfor

Conversation

@jwreschnig-utiq
Copy link
Copy Markdown
Contributor

No description provided.

@jwreschnig-utiq
Copy link
Copy Markdown
Contributor Author

Open to other namings - e.g. BindOf also seems somewhat idiomatic - but unfortunately the best would probably have been Bind.

options_test.go Outdated

var cli struct{}

p, err := New(&cli, BindFor(iface(impl("foo"))))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the concept, but this doesn't seem like a significant ergonomic improvement as it's still quite verbose and a bit unintuitive ☹️

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not so obvious in the test suite so maybe that needs clarification/a better example, but if you use BindFor(ctx) with a context.Context it will do the the right thing, which is the big win IMO.

When this infers the type of interfaces at compile-time it’s usually
more ergonomic.
@alecthomas alecthomas merged commit a2df5f5 into alecthomas:master Mar 31, 2026
@alecthomas
Copy link
Copy Markdown
Owner

Thanks!

@mitar
Copy link
Copy Markdown
Contributor

mitar commented Apr 1, 2026

Awesome. This works great for me and my use of fs.FS interface. I was able to replace:

kong.BindTo(dist.Files, (*fs.FS)(nil)))

With:

kong.BindFor(dist.Files),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants