Why are Span
methods like hours()
or minutes()
setters instead of getters?
#37
-
I'll preface this by saying that i'm quite a noob at Rust, so this might be a very stupid question. But, i found it weird when reading Jiff's docs that methods like This goes a bit contrary to what i thought was Rust's convention for getters/setters. I would've expected Why where these methods named this way? This document for API Guidelines confirmed this getter convention, but it says nothing about setters:
Some speculation / ideas... The only reason i can think of for having methods use jiff::ToSpan;
let span = 5.days().hours(8).minutes(1); Is this the reason for this design decision? If so, then i find it a bit unfortunate to end up with these not-very-conventional getters like If the idea is to use the extension methods on numbers as a more concise way of building Spans, then why not go all-in on these? use jiff::ToSpan;
let span = 5.days() + 8.hours() + 1.minutes(); And then we could have more conventional getters, like WDYT? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I answered what I believe is a related question in a more focused way in #41. The answer to that somewhat subsumes this question because it effectively rejects your suggestion of using operator overloading on addition to make constructing spans easier. With that said, it is technically separable from the problem of using the I use the pattern of the The main reason for this is that I perceive the getters in this case to be less frequently used---by a substantial amount---than the builder/setters. The Rust API guidelines are just that; guidelines. They aren't rules we must follow, and when the benefits of following them get dwarfed by the costs or the benefits of an alternative approach, then I firmly believe in feeling free to move away from the guideline. That's what I did here. I did not choose to use a Given that, I believe, overloading Someone did float the idea of adding free functions to Jiff for constructing single-unit spans, and this would avoid the prefix-suffix flipping used by the current Another choice is to have a separate |
Beta Was this translation helpful? Give feedback.
-
Thanks for the patience and thorough responses @BurntSushi!
Yeah, that's totally reasonable i think. And i agree, it's fine to break conventions in such cases where they'd do more harm than good. That being said, if the main (and maybe only) reason for having the shorter names like |
Beta Was this translation helpful? Give feedback.
I answered what I believe is a related question in a more focused way in #41. The answer to that somewhat subsumes this question because it effectively rejects your suggestion of using operator overloading on addition to make constructing spans easier.
With that said, it is technically separable from the problem of using the
get_
prefix in getters.I use the pattern of the
get_
prefix for getters inregex-automata
as well: https://docs.rs/regex-automata/latest/regex_automata/meta/struct.Config.htmlThe main reason for this is that I perceive the getters in this case to be less frequently used---by a substantial amount---than the builder/setters. The Rust API guidelines are just that; guid…