-
Notifications
You must be signed in to change notification settings - Fork 143
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
roboz0r
wants to merge
3
commits into
fsharp:main
Choose a base branch
from
roboz0r:fslang-892-units
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
```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? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 inverseLanguagePrimitives.FloatWithoutMeasure
, although the same effect is currently available with the existing primitive conversion methods likefloat
.Maybe the collections are better off in a
FSharp.Collections.Units
or as an addition toFSharp.UMX
that has an essentially identical API using overloaded methods for primitives extended to non-numeric types?