Skip to content
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

RFC FS-1148 - Ease conversion between Units of Measure (UoM) and undecorated numerals and simplify casting #784

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions RFCs/FS-1148-units.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# F# RFC FS-1148 - Ease conversion between Units of Measure (UoM) and undecorated numerals and simplify casting

The design suggestion [Ease conversion between Units of Measure (UoM) and undecorated numerals and simplify casting](https://github.com/fsharp/fslang-suggestions/issues/892) has been marked "approved in principle".

This RFC covers the detailed proposal for this suggestion.

- [x] [Suggestion](https://github.com/fsharp/fslang-suggestions/issues/892)
- [x] Approved in principle
- [x] [Implementation](https://github.com/dotnet/fsharp/pull/17518)
- [ ] Discussion

## Summary

A new type `Units` will be added to FSharp.Core and provide `static` methods for addition, removal and casting of UoM for supported primitive types and common collections containing primitive types.

## Motivation

Prior to this RFC, F# programmers have been able to apply units of measure supported primitive types through the use of functions in the `LanguagePrimitives` module e.g. `LanguagePrimitives.FloatWithMeasure<'M>`, or, by multiplying the value by one `1.0<kg>`. It has been commonly asked by the community for a simple way to remove units of measure from a given value or a way to convert units of measure from one unit to another directly. It has also been requested that there should be a way to convert collections of primitives e.g. `int[]` to `int<kg>[]` without having to create a copy of the collection.

## Detailed design

### FSharp.Core additions

The additions follow a common pattern:

- An overloaded `Add` method for each of the supported primitive types adds a UoM to the value
- An overloaded `Remove` method for each of the supported primitive types removed UoM from the value
- An overloaded `Cast` method for each of the supported primitive types replaces UoM on the value
- Overloaded `Add*`, `Remove*` and `Cast*` e.g. `AddArray` for common collection types containing supported primitives:
- `Array`
- `List`
- `ResizeArray`
- `Seq`

Total added methods is 195 `13 supported primitives * 3 methods * (primitive + 4 collection types)`. They are presented as 15 methods with 13 overloads each.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my personal concern - it's a lot of new surface area and probably a bunch of sigdata, optdata and il.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental size is around 31KB which translates to roughly 163 bytes per method. I don't really have a have a sense of whether that is acceptable or not. If the majority of F# users aren't making extensive use of UoM then there's no sense in making them carry around any extra weight.

The part of the feature that I sense would answer the most questions for users is removal of units from primitives. If I can call LanguagePrimitives.FloatWithMeasure, I'd expect the inverse LanguagePrimitives.FloatWithoutMeasure, although the same effect is currently available with the existing primitive conversion methods like float.

Maybe the collections are better off in a FSharp.Collections.Units or as an addition to FSharp.UMX that has an essentially identical API using overloaded methods for primitives extended to non-numeric types?


```fsharp
namespace Microsoft.FSharp.Core


type Units =
static member inline Add<[<Measure>]'Measure>(input: byte):byte<'Measure> = retype input
static member inline Add<[<Measure>]'Measure>(input: float):float<'Measure> = retype input
...
static member inline Remove<[<Measure>]'Measure>(input: byte<'Measure>):byte = retype input
static member inline Remove<[<Measure>]'Measure>(input: float<'Measure>):float = retype input
...
static member inline Cast<[<Measure>]'MeasureIn, [<Measure>]'MeasureOut>(input: byte<'MeasureIn>):byte<'MeasureOut> = retype input
static member inline Cast<[<Measure>]'MeasureIn, [<Measure>]'MeasureOut>(input: float<'MeasureIn>):float<'MeasureOut> = retype input
...
static member inline AddArray<[<Measure>]'Measure>(input: byte[]):byte<'Measure>[] = retype input
```

### Drawbacks

- `FSharp.Core` already offers some of the functionality in the `LanguagePrimitives` module however this has not been easy to discover for users.

### Alternatives

- Don't do this and maintain status quo
- Make the additions to `LanguagePrimitives`
- Use different type and/or method names
- Modify the F# language to provide a generic way to perform unit conversions - this would require the addition of `'T<'m>` as a language concept.

### Compatibility

This is not a breaking change. The elaboration of existing code that passes type checking is not changed.

This doesn't extend the F# metadata format.

### Unresolved questions

- Should any other collections be added?
- The use of overloaded methods is not typical for FSharp.Core design. Is it acceptable here?