49868

Creating a list with multiple units of measurements of floats in F#

Question:

So, I've tried to get around this in various ways, but I just can't make this work.

Is there any way to make a list that contains values of varying units of measurement (all based on floats)? For example:

let myList = [0.07<ms>; 0.9; 7.2<mm>;]

As they are treated as different types, you cannot put them in the same list. I tried declaring the list as let myList : float<_> list = ..., and giving dimensionless numbers a unit of measurement, but I still got a typing error: expecting float<'u> but got float.

I am unable to use a tuple/n-ple as I do not know the number of values that will be in the list.

I am quite new to F# and have spent quite a while scouring the documents and web on storage, but haven't found a solution. If anyone could point me in the right direction, I would really appreciate it. Thank you!

Answer1:

I think you'll need to give a longer example that shows how you want to use this list. Otherwise it is difficult to give a good answer, because it depends on the use.

If you just want to create a list of numbers that represent different things, then you can consider using a discriminated union to differentiate between them:

type Numeric = | Length of float<mm> | Time of float<ms> | Unitless of float let myList = [ Time 0.07<ms>; Unitless 0.9; Length 7.2<mm>;]

Then you can create a list that contains different numbers (with different physical meanings). When iterating over the list, you'll need to use pattern matching to extract the value.

Alternatively, you can just drop all units when creating the list, but then you lose the guarantees provided by units of measure (meaning that when you get some value from the list, you won't know what unit it had and you could interpret it wrongly):

let myList = [ float 0.07<ms>; 0.9; float 7.2<mm>;]

You could also use <a href="http://trelford.com/blog/post/Runtime-Units-of-Measure-for-F.aspx" rel="nofollow">an F# library that lets you track units at runtime</a>.

Answer2:

All members of a list <a href="http://msdn.microsoft.com/en-us/library/dd233224.aspx" rel="nofollow">must have the same type</a>. A float<ms> is not the same as a float<mm>.

Recommend

  • failure to adapt pymc2 into pymc3
  • Strange multiple assignment error in C++
  • Installing MEAN Stack: npm -v module.js: 338 throw err; Error: Cannot find module './cache/cach
  • Sharing Redis settings across routes
  • Why does NotifyIcon not set SynchronizationContext?
  • How to make a npm command, that executes two commands in the console (one after another)?
  • In VIm, how to remove all lines that are duplicate somewhere?
  • IE readonly textarea problem
  • Excel VBA Intersect
  • I don't get what's the difference between format() and … (python)
  • How to get the index of element in the List in c#
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • How to initialize context? [closed]
  • How to define an array of floats in Shader properties?
  • how to set to NULL all the filestream varbinary(max) fields?
  • Scala: Function returning an unknown type
  • Django Haystack Rebuild Index
  • Cannot access Public method in Web Control's Page_Load event
  • Android cannot disable cut copy paste
  • Can't remove headers after they are sent
  • Git describe fails to return most recent annotated tag
  • URLConnection doesn't work since API 10 and higher?
  • SAXReader not re-ecape characters
  • Unable to install Git-core+svn by MacPorts
  • How can I set a binding to a Combox in a UserControl?
  • Unable to decode certificate at client new X509Certificate2()
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Asynchronous UI Testing in Xcode With Swift
  • Illegal mix of collations for operation for date/time comparison
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Launch Runnable Jar from Web Start
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Build own AppleScript numerical error handling
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • need help with bizarre java.net.HttpURLConnection behavior
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass