-
Notifications
You must be signed in to change notification settings - Fork 17
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
Make it possible to add #[builder]
to existing code without changing the name of the function under the macro
#6
Comments
The main idea is that the function's name under the use bon::builder;
#[builder(expose_positional_fn = example)]
fn example_builder(x: u32, y: u32) {}
example(1, 2);
example_builder()
.x(1)
.y(2)
.call(); The positional function becomes hidden by default and the current name of the function is the name of the function that returns a builder. I can add this as an example to that section to avoid confusion, thanks! |
On the other hand.. It's understandable that you'd expect I can also imagine it as a feature where providing a |
I updated the docs with the example above. I'll keep this issue open as a feature request to implement the syntax in your proposal where the positional function's name stays the same and the |
Oh, I see, thank you for the example, that makes it clear to me how it is intended to be used. It seems obvious in retrospect, but thank you for clarifying! I guess the new syntax will be more intuitive, but we can make do without it until (if) there's a new major version. |
#builder
to an existing fn#[builder]
to existing code without changing the name of the function under the macro
I don't think a major version will be required for this feature. The default behavior should still be such that the function's name is used for the builder function. However when setting an explicit |
use bon::builder;
// The previous name of the positional function needs to be specified
// as the value for `expose_positional_fn`.
#[builder(start_fn = example_builder, expose_positional_fn = example)]
fn example(x: u32, y: u32) {}
// The positional function is available
example(1, 2);
// The builder syntax is available with the given name
example_builder()
.x(1)
.y(2)
.call(); Does the above compile today? That's the most awkward example I can think of that might be broken by the proposed change. I think that should still compile, but the // The previous name of the positional function needs *is inferred*
// as the value for `expose_positional_fn`.
#[builder(start_fn = example_builder, expose_positional_fn)]
fn example(x: u32, y: u32) {} |
#[builder(start_fn = example_builder, expose_positional_fn = example)] Such syntax should be a compile error, because now there are 3 names involved, when only 2 are needed.
It doesn't because |
Hey @drmason13 I've just found a probably unrelated bug, but I want to make sure the fix won't affect your code. The bug is that when you place Instead the positional method I prepared a fix for that #19, that will be released as a new patch |
I haven't used this crate properly yet so there's no risk of breakage for me, but I appreciate the concern :) |
I think the syntax here should be the following. The
I can include this change in the 3.0 release of bon, which is going to have some other minor breaking changes as well (e.g. #145) |
Hi, sorry, it takes a lot of time to get this resolved just because I'm thinking too much about it and questioning the design again and again. Howerver, this issue is my current priority for The case of the method For example, with the current With the However, this design doesn't look intuitive with the method named At this point, I think I'm just prepared to the fact that Currently, there are around ~100 open source repos that use The documentation of Will I regret this in the future? I don't know. But if I or someone else discovers a better design for this at some point in the future I'll for sure implement that and deprecate the existing one. If you have any feedback I'd be glad to hear it! |
I'm struggling to grasp all of this, I will need some time to tinker and get a feeling for how it works. I'll try and make some before and after comparisons when various attributes are added and then let you know my thoughts. |
Sure, I'll ping you once I have a |
In the meantime, @drmason13 , maybe you could share what specific use case you have for exposing the positional function? Is this required for only backwards compatibility, or do you have other motivations? |
I might not have made this clear, but my question is mostly hypothetical. If someone needed backwards compatibility, how would they go about it. |
I see, thanks for clarifying |
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.
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 useexample_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:
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.
The text was updated successfully, but these errors were encountered: