Skip to content

Make it possible to add #[builder] to existing code without changing the name of the function under the macro #6

@drmason13

Description

@drmason13

First of all, thank you for this fantastic crate, I love the idea of functions and structs sharing a builder derive, and the documentation is brilliant, the big effort that went into it all is clear to see.

I read through
https://elastio.github.io/bon/docs/guide/compatibility#adding-builder-to-existing-code

and followed the link to

https://elastio.github.io/bon/docs/reference/builder#expose-positional-fn

but I couldn't see how to make the preserved fn backwards compatible after adding the builder.

use bon::builder;

#[builder(expose_positional_fn = example_positional)] 
fn example(x: u32, y: u32) {}

// Positional function is now available under the given name
example_positional(1, 2);                                     

// Builder syntax is also available (unchanged)
example()
    .x(1)
    .y(2)
    .call();

Old code would do example(1, 2) and expect the function result, not a builder type. It is a small breaking change, as code could be changed to use example_positional(1, 2) instead easily with find/replace.

Would it be possible to change the name of the builder and keep the original function unchanged? E.g. for this to work:

use bon::builder;

#[builder(builder_fn = example_builder, expose_positional_fn)] 
fn example(x: u32, y: u32) {}

// Positional function is now available under the *original* name
example(1, 2);                                     

// Builder syntax is also available with the *explicit* name
example_builder()
    .x(1)
    .y(2)
    .call();

I'm not sure I want this as a feature request, but I think it deserves calling out specifically that expose_positional_fn is useful for "easing the breakage" but not able to provide backwards compatibility - if indeed that is the case.

I would add that statement to the compatibility section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions