Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

c# - Reflection for F# units of measure

Support for reflection has been currently added into F#, but it is not working for measure types. Is it possible to use reflection in F# for measure types? I've read this. It was for 2008, but if you check some code like bellow in ildasm you cannot see anything about Units of Measure.

// Learn more about F# at http://fsharp.net

[<Measure>] type m
[<Measure>] type cm
 
let CalculateVelocity(length:float<m> ,time:float<cm>) =
    length / time

The ildasm output:

.method public static float64  CalculateVelocity(float64 length,
                                                 float64 time) cil managed
{
  // Code size       5 (0x5)
  .maxstack  4
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  div
  IL_0004:  ret
} // end of method Program::CalculateVelocity

So there are somethings that cannot be reflected in F#. Is it true or not? see the comment : Units actually don't get seen at all by the CLR ... in the article.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others already pointed out, when you need to get some information about compiled F# types, you can use standard .NET reflection (System.Reflection) and F# reflection which provides information about discriminated unions, records, etc. (Microsoft.FSharp.Reflection).

Unfortunatelly, information about units of measure cannot be accessed using any of these two APIs, because they are checked only during the compilation and do not actually exist at runtime (they cannot be represented in the CLR in any way). This means that you'll never be able to find out whether e.g. a boxed floating point value has some unit of measure...

You can get some information about units of measure using Metadata namespace from F# PowerPack. For example, the following prints that foo is a unit:

namespace App
open System.Reflection
open Microsoft.FSharp.Metadata

[<Measure>] 
type foo

module Main = 
  let asm = FSharpAssembly.FromAssembly(Assembly.GetExecutingAssembly())
  for ent in asm.Entities do
    if ent.IsMeasure then
      printfn "%s is measure" ent.DisplayName

This reads some binary metadata that the compiler stores in compiled files (so that you can see units when you reference other F# libraries), so you should be able to see informaiton about public API of F# libraries.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...