Skip to content

Instantly share code, notes, and snippets.

@hafuu
Created February 5, 2016 19:18
Show Gist options
  • Save hafuu/a465449c13511a574af0 to your computer and use it in GitHub Desktop.
Save hafuu/a465449c13511a574af0 to your computer and use it in GitHub Desktop.
InferredFloatRepro.fs
module InferredFloatRepro
open Microsoft.FSharp.Compiler.SourceCodeServices
open System.IO
let inferredFloatFunction x = 0.0 + x
let annotatedFloatFunction (x: float): float = 0.0
let inferredIntFunction x = 0 + x
type Dummy = Dummy
let loadAssembly () =
let asm = typeof<Dummy>.Assembly
let checker = FSharpChecker.Create()
let base1 = Path.GetTempFileName()
let fileName1 = Path.ChangeExtension(base1, ".fs")
let projFileName = Path.ChangeExtension(base1, ".fsproj")
let dllName = Path.ChangeExtension(base1, ".dll")
let options =
let syslib name =
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
+ @"\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\"
+ (name + ".dll")
let fscore4400 =
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
+ @"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.4.0.0\FSharp.Core.dll"
checker.GetProjectOptionsFromCommandLineArgs
(projFileName,
[| yield "--simpleresolution"
yield "--noframework"
yield "--debug:full"
yield "--define:DEBUG"
yield "--optimize-"
yield "--out:" + dllName
yield "--warn:3"
yield "--fullpaths"
yield "--flaterrors"
yield "--target:library"
yield fileName1
let defaultReferences =
[ syslib "mscorlib"
syslib "System"
syslib "System.Core"
syslib "System.Xml"
syslib "System.Configuration"
fscore4400
asm.Location ]
for r in defaultReferences do
yield "-r:" + r |]
)
let x = checker.ParseAndCheckProject(options) |> Async.RunSynchronously
x.ProjectContext.GetReferencedAssemblies()
|> Seq.find (fun x -> x.SimpleName = asm.GetName().Name)
let test() =
let assembly = loadAssembly()
let thisModule = assembly.Contents.Entities |> Seq.find (fun x -> x.DisplayName = "InferredFloatRepro")
let test name (x: FSharpMemberOrFunctionOrValue) =
let arg = x.FullType.GenericArguments.[0]
printfn "%s.IsAbbreviation: %b" name arg.IsAbbreviation
printfn "%s.AbbreviatedType: %A" name arg.AbbreviatedType
printfn "%s.TypeDefinition.TryFullName: %A" name arg.TypeDefinition.TryFullName
printfn "%s.GenericArguments: %A" name (arg.GenericArguments |> Seq.map (fun x -> x.TypeDefinition.DisplayName))
test "inferredFloat" (thisModule.MembersFunctionsAndValues |> Seq.find (fun x -> x.DisplayName = "inferredFloatFunction"))
test "annotatedFloat" (thisModule.MembersFunctionsAndValues |> Seq.find (fun x -> x.DisplayName = "annotatedFloatFunction"))
test "inferenceInt" (thisModule.MembersFunctionsAndValues |> Seq.find (fun x -> x.DisplayName = "inferredIntFunction"))
()
@hafuu
Copy link
Author

hafuu commented Feb 5, 2016

execution result

inferredFloat.IsAbbreviation: false
inferredFloat.AbbreviatedType: type System.Double
inferredFloat.TypeDefinition.TryFullName: Some "Microsoft.FSharp.Core.float`1"
inferredFloat.GenericArguments: seq ["MeasureOne"]
annotatedFloat.IsAbbreviation: true
annotatedFloat.AbbreviatedType: type System.Double
annotatedFloat.TypeDefinition.TryFullName: <null>
annotatedFloat.GenericArguments: seq []
inferenceInt.IsAbbreviation: true
inferenceInt.AbbreviatedType: type System.Int32
inferenceInt.TypeDefinition.TryFullName: <null>
inferenceInt.GenericArguments: seq []

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