Allow units of measure in for loops #534

Open
marklam opened this Issue Feb 9, 2017 · 1 comment

Projects

None yet

2 participants

@marklam
marklam commented Feb 9, 2017 edited

Allow units of measure in for loops

I propose we extend the for loop typing to allow looping over an integer with units of measure.
Currently, the following won't compile:

let maximumDistance = 3<km>
let minimumDistance = 1<km>
for trial = minimumDistance to maximumDistance do
    goRunning (trial * 1<km>)

The existing way of approaching this problem is to cast away the units of measure, and multiply them back in within the loop, which is ugly and reduces the safety that uom adds.

Pros and Cons

Advantages:

  • An improvement to the usefulness of units of measure

Disadvantages:

  • Maybe this would make loop optimization more tricky?

Extra informtion

Estimated cost (XS, S, M, L, XL, XXL):
S to M ?

Related suggestions: (put links to reated suggestions here)
Allow units of measure in sequence ranges #535

Affadavit (must be submitted)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I would be willing to help test this
  • I or my company would be willing to help crowdfund F# Software Foundation members to work on this
@marklam marklam referenced this issue Feb 9, 2017
Open

Allow units of measure in sequence ranges #535

5 of 6 tasks complete
@rmunn
rmunn commented Feb 20, 2017

As someone mentioned in #535, the for loop in this suggestion can currently be written as follows:

let maximumDistance = 3<km>
let minimumDistance = 1<km>
for trial in minimumDistance .. 1<km> .. maximumDistance do
    goRunning trial

Note that there's no need to cast trial inside the loop in this version, because it now has the unit of measure km.

However, it would still be nice to be able to do for trial in minimumDistance .. maximumDistance or for trial = minimumDistance to maximumDistance without having to explicitly specify the step as a UoM. This seems like a case where the compiler should be able to Do The Right Thing automatically, inferring the correct unit of measure for trial and for the step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment