Skip to content

Introduce the syntax where builder is passed via a closure #127

@Veetaha

Description

@Veetaha

This is based on a comment from @cksac in #85 (comment)
It should look something like this. An additional config should enable the generation of a separate syntax for the builder where all required parameters are passed as positional parameters at the start of the function and optional parameters configured with the builder passed as an input to the trailing closure argument.

    use bon::bon;

    struct Tensor {}

    #[bon]
    impl Tensor {
        #[builder(closure_fn = sum_with)]
        fn sum(
            &self,
            dims: &[i64],
            #[builder(default)] keep_dim: bool,
            #[builder(default)] mean: bool,
        ) -> Tensor {
            Tensor {}
        }

    }

    let t = Tensor{};

    let output = t
        .sum_with(&[1, 2], |b| b.keep_dim(true).mean(false))
        // Automatically sets all optional parameters to default values
        .sum(&[1]);

I think this API looks too exotic, and it's probably fine if it would require a bit more code to write for the user. For example, if we just generate a From<TBuilder> for T impl, this API can be written manually using existing bon primitives (see the comment #85 (comment)).

A note for the community from the maintainers

Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    design neededThe feature requires more design effortfeature requestA new feature is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions