-
Notifications
You must be signed in to change notification settings - Fork 36
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
Consider adding builder methods that take Options #115
Comments
We had discussed this when designing the current API but favored simplicity of the API as people who needed This doesn't mean it can't be re-visited but
|
I guess reasonable people can agree to disagree here. :) My intuition is that production code would tend to have more complicated logic and expressions to calculate what to pass for an I also don't think of it as a very big simplicity win to allow people to omit All that said, I recognize that:
So in the end you're certainly in a better position to make decisions, and I can still get what I want with a handful of lines of trivial code, so I'm not going to put up much of a fight for changing it. I'm content to just agree to disagree. I definitely agree with you about two things:
Thanks for replying and listening. |
Have you considered using pub fn origin<T>(mut self, origin: T) -> Self where T: Into<Option<&'a str>> {
self.origin = origin.into();
self
} I believe that allows all of: |
One problem with |
Good point! Guess it's more of a trade-off than I thought. |
In general the new API in 0.11 works nicely, but one pain point I had was mapping
Option<Foo>
values (which I either calculate on the fly or have stored in a data structure) to optional builder method calls. For example, I'll have an expression which results in anOption<&str>
for the origin of aSnippet
, whereNone
means that it won't have one. This worked great in the 0.10.x API because I could just assign my value to the relevant field of the annotate-snippets data structure, since it was also anOption
. In the new 0.11.1 API I now either need to callSnippet.origin(...)
(if my value isSome
) or not call it (if my value isNone
), which really breaks the nice ergonomics of chaining all of the needed builder method calls.For example:
Consider the potential improvement if there was a builder method that took an
Option
:Now the entire thing is a single expression -- which could e.g. be passed straight to another builder function -- instead of requiring a temporary mutable variable and conditional logic.
It's also interesting to consider the inconsistency with the
Snippet::fold
method, which doesn't have this problem. I can have abool
value (or expression) that controls whether folding should be enabled or disabled and pass it straight tofold
rather than needing logic to either callfold
(in thetrue
case) or not call it (in thefalse
) case. Iffold
were consistent with the other builder methods as they currently are, then it would take no parameters and always turn folding on when called. To be clear, though, I'm not advocating that you should changefold
to be that way; I'm actually advocating the opposite, that you change the other builder methods (or add additional ones) for consistency with howfold
currently is, i.e. allow them to be used to explicitly specify the default value.My workaround for this was to make little extension traits and add my own
maybe_*
versions of relevant builder methods (so far justSnippet::maybe_origin
andAnnotation::maybe_label
). While that's an entirely technically valid solution, I feel like there's a pretty good chance that such builder methods would end up being used often enough as to warrant their inclusion in the crate's API.The text was updated successfully, but these errors were encountered: