-
Notifications
You must be signed in to change notification settings - Fork 2
OmnICU API surface #15
Comments
I'm in favor of (1). I just finished implementing custom types and formatters for I think that a call like: DateTime::new(41312314141, DateTimeOptions {
date_style: DateTimeStyleValues::Full,
time_style: DateTimeStyleValues::Medium,
hour_cycle: DateTimeStyleHourCycles::H24
}); is more rusty than a builder pattern. It also allows us to do more with the options struct - clone, serialize, parse from JSON, etc. |
@filmil suggested another alternative: an array of enums. Something like this: mod NumberFormat {
#[derive(Debug)]
pub enum Opt<'a> {
style(Style),
currency(&'a str),
maximumFractionDigits(i32),
}
pub fn build(locale: &str, options: &[Opt]) {
print!("{:?} {:?}", locale, options);
}
}
fn main() {
NumberFormat::build("pt-BR", &[
NumberFormat::Opt::style(NumberFormat::Style::CURRENCY),
NumberFormat::Opt::currency("EUR"),
]);
} |
That seems semantically weak. You need to handle all cases of: NumberFormat::build("pt-BR", &[
NumberFormat::Opt::style(NumberFormat::Style::CURRENCY),
NumberFormat::Opt::style(NumberFormat::Style::DECIMAL),
]); and eventually, you'll want to parse it into some semantically readable structure with a single What's the benefit of that over: NumberFormat::new("pt-BR", NumberFormat::Options {
style: NumberFormat::Style::CURRENCY,
currency: "EUR"
}); For |
I'd also argue that we should do: let langid: LanguageIdentifier = "pt-BR".parse().unwrap();
NumberFormat::new(langid, NumberFormat::Options {
style: NumberFormat::Style::CURRENCY,
currency: "EUR"
}); or have a fallible constructor: impl NumberFormat {
pub fn try_new(loc: impl TryInto<LanguageIdentifier>) -> Result<Self, ()>;
} to allow for parsing of the locale to fail, or the locale to not be available - it's also worth here talking about where the language negotiation should happen. I'm not a huge fan of the implict negotiation, and would prefer an explicit one:
|
Fair point, and I don't insist on my proposed aprpoach. Which reminds me: could you recommend other similarly configured APIs that we can take a look and learn from? |
If you refer to decoupling langid parsing from formatter initialization, that seems reasonable to me too, as a way to separate the parsing and formatting concerns
IMHO this makes
ECMA402 has an abstract operation |
There are three reasons why constructor like this may fail:
===
I'd like to aim for a simple raw low-level API design where negotiation is either explicit, or explicitly visible from the callsite.
Finally, there are errors that come from a failure of combining options with data. Let's say I construct a formatter for All those cases make me feel that constructor should be fallible, and then we may consider making |
This discussion was continued on unicode-org/icu4x#112 and we decided to move forward with the options struct and default trait. |
I don't know if the other discussions in this thread are still open. Can you open follow-up issues in the ICU4X repo if necessary? |
I made a doc discussing the API surface for OmnICU in several host languages. I have a section about Rust.
https://docs.google.com/document/d/1tXACn0p2EuzSCJ0Gd8Nd1RV-DgdYG54N6lDwqYeQE4Q/edit#
One big open question is about named arguments delivery. In ECMAScript, we just use object literals and it's easy. In Rust, I suggested two options:
Thoughts?
The text was updated successfully, but these errors were encountered: