F#版本更新说明
F#CTP 1.9.6.0更正列表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FSharp.Core;
namespace TestFSharpTuple
{
public class Class1
{
public static Tuple<int, int, int, int, int> T1
{
get { return new Tuple<int, int, int, int, int>(1, 2, 3, 4, 5); }
}
public static Tuple<int, int, int, int, int, int, int> T2
{
get { return new Tuple<int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7); }
}
public static Tuple<int, int, int, int, int, int, TupleEnd<int>> T3
{
get { return new Tuple<int, int, int, int, int, int, TupleEnd<int>>(1, 2, 3, 4, 5, 6, new TupleEnd<int>(7)); }
}
public static Tuple<int, int, int, int, int, int, TupleNested<int>> T4
{
get
{
return new Tuple<int, int, int, int, int, int, TupleNested<int>>
(1, 2, 3, 4, 5, 6, new TupleNested<int>(7));
}
}
public static Tuple<int, int, int, int, int, int, TupleNested<Tuple<int, int>>> T5
{
get
{
return new Tuple<int, int, int, int, int, int, TupleNested<Tuple<int, int>>>
(1, 2, 3, 4, 5, 6, new TupleNested<Tuple<int, int>>(new Tuple<int, int>(7, 8)));
}
}
public static Tuple<int, int, int, int, int, TupleNested<Tuple<int, int, int>>> T6
{
get
{
return new Tuple<int, int, int, int, int, TupleNested<Tuple<int,int, int>>>
(1, 2, 3, 4, 5, new TupleNested<Tuple<int,int, int>>(new Tuple<int,int, int>(6,7, 8)));
}
}
}
}
printfn "%A" TestFSharpTuple.Class1.T1
printfn "%A" TestFSharpTuple.Class1.T2
printfn "%A" TestFSharpTuple.Class1.T3
printfn "%A" TestFSharpTuple.Class1.T4
printfn "%A" TestFSharpTuple.Class1.T5
printfn "%A" TestFSharpTuple.Class1.T6
1、名称空间必须使用完全限定名打开,名称空间不可以取别名。
2、在fs,fsi文件中,除注释外,第一行必须使用#light或#light "off"
3、元组类型互操作上发生了变动。
新增了两个类型来提供互操作上的支持。TupleEnd,TupleNested,彻底解决了互操作上的不一致性。
let tuple_7=(1,2,3,4,5,6,7)
let tuple_9=(1,2,4,8,16,32,64,128)
let tuple_7_1=(1,2,4,8,16,32,(64,128))
let tuple_9=(1,2,4,8,16,32,64,128)
let tuple_7_1=(1,2,4,8,16,32,(64,128))
对应的c#签名:
public static Microsoft.FSharp.Core.Tuple<int, int, int, int, int, int, Microsoft.FSharp.Core.TupleEnd<int>> tuple_7 { get; }
public static Microsoft.FSharp.Core.Tuple<int, int, int, int, int, int, Microsoft.FSharp.Core.TupleEnd<Microsoft.FSharp.Core.Tuple<int, int>>> tuple_7_1 { get; }
public static Microsoft.FSharp.Core.Tuple<int, int, int, int, int, int, Microsoft.FSharp.Core.TupleNested<Microsoft.FSharp.Core.Tuple<int, int>>> tuple_9 { get; }
public static Microsoft.FSharp.Core.Tuple<int, int, int, int, int, int, Microsoft.FSharp.Core.TupleEnd<Microsoft.FSharp.Core.Tuple<int, int>>> tuple_7_1 { get; }
public static Microsoft.FSharp.Core.Tuple<int, int, int, int, int, int, Microsoft.FSharp.Core.TupleNested<Microsoft.FSharp.Core.Tuple<int, int>>> tuple_9 { get; }
C#语言如果使用以下的规则,F#语言就可以正确的识别出元组。
如果元组元素个数小于7,使用Tuple泛型,下面的代码,F#可以正确的转换为(1,2,3);
new Tuple<int,int,int>(1,2,3)
等于七个元素,第七个元素使用TupleEnd包装,下面的代码,F#可以正确的转换为(1,2,3,4,5,6,7)
net Tuple<int,int,int,int,int,int,TupleEnd<int>)(1,2,3,4,5,6,new TupleEnd<int>(7))
大于七个元素的元组,七个元素以上的使用TupleNested包装,下面的代码,F#可以正确的转换为(1,2,3,4,5,6,7,8)。
new Tuple<int, int, int, int, int, int, TupleNested<Tuple<int, int>>>
(1, 2, 3, 4, 5, 6, new TupleNested<Tuple<int, int>>(new Tuple<int, int>(7, 8)))
(1, 2, 3, 4, 5, 6, new TupleNested<Tuple<int, int>>(new Tuple<int, int>(7, 8)))
为了给大家一个直观的映象,我给出完整的调用例子。
首先请建立一个c#类库工程,引用FSharp.Core.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FSharp.Core;
namespace TestFSharpTuple
{
public class Class1
{
public static Tuple<int, int, int, int, int> T1
{
get { return new Tuple<int, int, int, int, int>(1, 2, 3, 4, 5); }
}
public static Tuple<int, int, int, int, int, int, int> T2
{
get { return new Tuple<int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7); }
}
public static Tuple<int, int, int, int, int, int, TupleEnd<int>> T3
{
get { return new Tuple<int, int, int, int, int, int, TupleEnd<int>>(1, 2, 3, 4, 5, 6, new TupleEnd<int>(7)); }
}
public static Tuple<int, int, int, int, int, int, TupleNested<int>> T4
{
get
{
return new Tuple<int, int, int, int, int, int, TupleNested<int>>
(1, 2, 3, 4, 5, 6, new TupleNested<int>(7));
}
}
public static Tuple<int, int, int, int, int, int, TupleNested<Tuple<int, int>>> T5
{
get
{
return new Tuple<int, int, int, int, int, int, TupleNested<Tuple<int, int>>>
(1, 2, 3, 4, 5, 6, new TupleNested<Tuple<int, int>>(new Tuple<int, int>(7, 8)));
}
}
public static Tuple<int, int, int, int, int, TupleNested<Tuple<int, int, int>>> T6
{
get
{
return new Tuple<int, int, int, int, int, TupleNested<Tuple<int,int, int>>>
(1, 2, 3, 4, 5, new TupleNested<Tuple<int,int, int>>(new Tuple<int,int, int>(6,7, 8)));
}
}
}
}
在F#中分别调用
printfn "%A" TestFSharpTuple.Class1.T1
printfn "%A" TestFSharpTuple.Class1.T2
printfn "%A" TestFSharpTuple.Class1.T3
printfn "%A" TestFSharpTuple.Class1.T4
printfn "%A" TestFSharpTuple.Class1.T5
printfn "%A" TestFSharpTuple.Class1.T6
你会发现调用T2,T4均出错。T6不会出错,但可能不是你想要的结果,T6的结果为(1,2,3,4,5,(6,7,8))
调用T2时的错误截图:
调用T4的错误截图: