Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Builder closures as args (nested/sub builders) #188

Open
polarathene opened this issue Jun 6, 2024 · 0 comments
Open

Comments

@polarathene
Copy link

I was curious about a better approach to this awkward separate builder example:

// fn new(pair: Pair<String, String>) -> Self
builder().pair(
    Pair::builder().x("hello").y("world").build()
).build();

The DX could be improved perhaps by supporting closures to abstract another builders entry (T::builder()) + exit (.build()) calls?:

// Separate builder for `Pair`:
// fn new_pair(x: String, y: String) -> Pair<String, String>

// type signature for `pair` probably needs to change for builders that leverage it?:
// fn new(pair: Pair<String, String>, another_param: Option<bool>) -> Self
builder().pair(|p| {
    p.x("hello").y("world")
})

Nesting builders with sub-builders would seem a bit nicer with that approach if it's viable?


This is a bit related to issues:

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

No branches or pull requests

1 participant