code-ape/rust_length_arithmetic_example

plural `s`

Opened this issue · 2 comments

I just read your intersting blog post and stumbled upon the plural s logic in your example.

I think there is a general problem and an easy solution:

  • not all languages have a plural s, e.g. the German plural of meter is meter.
  • even in the English language there are irregular words. The plural of foot is feet and the plural of Celsius is Celsius :-)

In other words the FooUnit traits should have a singular_name() and plural_name() method. Also the return type could be changed to &'static str to reflect the fact that it is a constant and avoid the heap allocation of String.

Alternatively you could add an overridable fmt method to the LengthUnit and other FooUnit traits: fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result. By default it would do the plural-s logic. But some Units like foot or inch could override it.

This pattern would be really useful for units which behave like currencies where it is $1 but 1 €. While it doesn't make sense to hardcode the exchange rates, there might be other units which have similar display quirks. :-)

@colin-kiegel: Thank you for taking the time to give feedback on this! You're absolutely right. When I wrote the blog post I was thinking about metric units in English but there are many cases where the logic I wrote doesn't hold up. I'll look to update my blog post and this code with a comment related to think soon and an acknowledgement to you :)