-
Notifications
You must be signed in to change notification settings - Fork 5
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
Should the API for creating the date components be updated? #93
Comments
Are you referring to this? Lines 308 to 314 in a7fc946
Yeah I guess this could work, I don't have any issue with the idea, I'm not sure it's immediately obvious though so it would need some documentation around it.
Yep same, it feels odd, with some documentation we may be OK. I actually don't mind how it is right now, its explicit |
More so around the specific components like impl Date {
// Creates a `Date`, constraining to the nearest valid Date if the values are not in a valid range.
fn new(
year: i32,
month: i32,
day: i32,
calendar: Calendar,
) -> Self {
Date::new_with_option(feilds, ArithmeticOverflow::Constrain)
.expect("Constraining cannot return an error.")
}
/// Tries to create a new Date, throwing an error if the values are not in a valid Date range.
fn try_new(
year: i32,
month: i32,
day: i32,
calendar: Calendar,
) -> TemporalResult<Self> {
Date::new_with_option(feilds, ArithmeticOverflow::Reject)
}
/// Create a `Date` with a provided arithmetic overflow option.
fn new_with_option(
year: i32,
month: i32,
day: i32,
calendar: Calendar,
option: ArithmeticOverflow,
) -> TemporalResult<Self> {
todo!()
}
}
|
Sorry yes that’s what I meant (that try_new just calls it with reject). Would these be convenience methods with |
I think We could also feature flag them with a feature as default that then allows us to remove them when implementing the engine as they'd probably not be needed. |
I would be fine with this change, if its documented well enough |
In general, this completes some various API housekeeping that was needed. Updates the built-ins to align with specification names, changes the exports, implements some of the general API changes to be more Rust-like. Below are a full list of changes: - Updates all component names to align with the Temporal spec (Adds the `Plain` prefix where needed) - Initial implementation of the API suggestions from #93 - Exports each component in the libraries root and adds the partial records to a `partial` mod. Ideally, now working with the library should look something like the below. ```rust // previously would have been: // use temporal_rs::{components::{calendar::Calendar, Date, PartialDate}, options::ArithmeticOverflow}; use temporal_rs::{Date, Calendar, partial::PartialDate, options::ArithmeticOverflow}; let iso_calendar = Calendar::from_str("iso8601")?; // Previously would have been Date::new(2020, 10, 19, iso_calendar.clone(), ArithmeticOverflow::Reject)?; let constructed_date = Date::try_new(2020, 10, 19, iso_calendar.clone())?; let today = PartialDate { year: Some(2020), month: Some(10), day: Some(19), ..Default::default() }; let date_from_partial = iso_calendar.date_from_partial(&today, ArithmeticOverflow::Reject)?; assert_eq!(constructed_date, from_partial); ```
Currently, to create one of the structs, our general API is
new
with an overflow option. It may be better to have a more "rusty" API; i.e., we lack atry_new
method.General Background:
The Temporal specification provides the option to
Constrain
orReject
the creation of an object withRegulateX
abstract methods. To capture this, the currentnew
methods all have anoption
parameter that allows choosing the overflow option.While the current
new
method provides those options, it also means the API doesn't follow typical conventions that may be expected by any Rust users and may also be a little vague.Initial Proposal:
Essentially, my thinking is that we should add a
try_new
method and update the API to be a bit more explicit.new(fields..., overflow_option)
=>new_with_overflow_option(fields, overflow_option)
try_new(fields ...)
that callsnew_with_overflow_option(fields..., REJECT)
new(fields...)
that callsnew_with_overflow_option(fields..., CONSTRAIN)
That's sort of along the lines of what I was thinking, but I thought I'd bring it up for discussion. I'm not exactly in love with the idea of
new
being constrain by default, but I think it'd be fine as long as it's documented.Any thoughts?
The text was updated successfully, but these errors were encountered: