2011-11-08 47 views
8

在F#交互,下面的代码片的工作原理:十进制的转换F#脚本与编译F#

> printfn "%A" (decimal 1I) 
1M 

然而,在已编译的F#的程序,将显示错误消息:

The type 'Numerics.BigInteger' does not support a conversion to the type 'decimal' 

那里发生了什么?是否因为在F#版本之间使用了不同的引用集(以及引用版本)?或decimal的内部表示在编译和解释模式下不同。

回答

9

这可能是因为您的F#编译程序针对.NET Framework 2.0/F#2.0。 F#交互式使用.NET Framework 4.0/F#4.0。

2.0 Framework在FSharp.Core中使用BigInteger。 4.0框架使用System.Numerics.BigInteger。 FSharp.Core一个没有转换为十进制。

将您的项目更改为目标.NET 4.0并添加对System.Numerics的引用,并且所有内容都应匹配。

+2

+1你打我吧。我会在那里留下我的答案,以防一些额外的细节有用。 –

2

你说得对,BigInteger是否可以使用decimal函数转换或不转换。它似乎取决于您正在编译的.NET版本。如果您使用Visual Studio 2010中的F#编译器(或F#交互),则默认目标是.NET 4.0。对于这一目标,汇编工作正常:

C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs 
Microsoft (R) F# 3.0 Compiler build 2.0.0.0 
Copyright (c) Microsoft Corporation. All Rights Reserved. 

您可以通过显式引用.NET 2.0的mscorlib.dllFSharp.Core.dll版本更改目标框架。然后编译器报告您所描述的错误:

C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs --noframework 
    -r:C:\Program Files (x86)\FSharp-2.0.0.0\bin\FSharp.Core.dll 
    -r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll 
Microsoft (R) F# 3.0 Compiler build 2.0.0.0 
Copyright (c) Microsoft Corporation. All Rights Reserved. 

test.fs(1,23): error FS0001: The type 'System.Numerics.BigInteger' does not support 
    a conversion to the type 'decimal' 

如果你编译项目时收到错误,那么你的项目很可能配置为编译为.NET 2.0。

0

同样的结果

Microsoft(R) F# 2.0 Interactive ビルド 4.0.40219.1 
Copyright (c) Microsoft Corporation. All Rights Reserved. 
> printfn "%A" (decimal 1I);; 
1M 
val it : unit =() 

>fsc test.fs 
Microsoft(R) F# 2.0 Compiler ビルド 4.0.40219.1 
Copyright (c) Microsoft Corporation. All Rights Reserved. 

>test 
1M