30 Aug 2008 @ 6:07 下午 
 

F# CTP 1.9.6.0 发行说明(Release Notes)摘要

 

原文链接:

http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx

先说说我自己的试用体验:

  • 源代码可以排序了, 右键菜单上虽然没有显示快捷键, 但是可以通过按 ALT + Up/Down 来换位.
  • CTRL + E, C 注释的时候不再是 (* *) 了, 而是与 C# 相同的多行 //.
  • CTRL + K, F 代码格式化仍然不可用, 哭...
  • 智能感知比之前流畅了些, 但仍有待提高, 回车换行后仍然不能自动缩进.

工具:

项目文件

  • 项目文件扩展名改为 .fsproj 并且支持 MSBuild.

引用文件

  • #r 引用现在只能用于 .fsx 文件
  • 在 Windows 平台上使用 #r 引用注册表 AssemblyFoldersEx 中的程序集时不必指定路径. (指在添加引用对话框中默认可见的程序集, 例如可以直接 #r "System.Xml.Linq.dll")
  • 默认情况下脚本是不包含在项目中的(不参与编译), 而且你必须另外详尽的指定所引用的程序集(项目引用不等于脚本能用).
  • #load 对智能感知也会有作用.
  • #use 被移除了.

编译器命令行

  • --generate-interface-file 改为 --sig
  • --no-warn 改为 --nowarn
  • -O3 改为 -optimize 或 -O

语言:

度量单位的推导与检查

  • [<Measure>]
    type 米
    [<Measure>]
    type 秒

    let dist = 100.0<米>
    let time = 9.69<秒>
    let speed = dist / time   // val it : float<米/秒> = 10.31991744
    // let foo = dist + time   // 错误: The unit of measure '秒' does not match the unit of measure '米'

轻量级语法

  • #light 和 #light "off" 在 .fs .fsx .fsi 文件中是必须的, 但在 .ml 和 .mli 文件中隐含为 #light "off".

隐式转换

  • 可能之前你已经听说过 F# 放弃隐式转换的一个理由是类型推断, 但是适当的时候如果能实现自动 Upcast 也不错:
    // Class
    type Base() =
        member b.X = 1
    type Derived(i : int) =
        inherit Base()   // 顺便说句: 继承之后的 as base 已经不可用了, 默认即为 base
        member d.Y = i
    let d = new Derived(7)
    let f (b : Base) = b.X
    let res = f d

    // Record
    type Rec = { A : Base ; X : int }
    let r = { A = new Derived(7); X = 12}

    // Union
    type Uni = A of Base | X of int
    let u = A(new Derived(7))

    // Mutable field
    type Rec2 = { mutable A : Base ; X : int }
    let r2 = { A = new Derived(7); X = 12}
    r2.A <- new Derived(3)

  • 以下情况不适用:
    // tuple creation
    // list elements
    // function binding and "return types"
    // if/then/else

改进工程实践

  • #nowarn 的范围扩大到文件末尾 (为什么不是项目末尾?)
  • open 打开命名空间必须指定完整路径
    open System
    open Windows   // 警告
    open Forms   // 警告
    open System.Windows.Forms   // 正确

命名空间缩写

  • 模块缩写不能用于命名空间
    module WF = System.Windows.Forms   // 错误 因为 Forms 不是模块.

assert(false)

  • assert 现在与 System.Diagnostics.Debug.Assert 相同, 在 Release 版本中忽略.

AutoOpenAttribute

  • // 当命名空间被打开时, 模块也随之自动打开.
    namespace ViTarn
    [<AutoOpen>]
    module Utils

支持 COM 自动和可选参数

操作符的定义更加简单

  • type Receiver(latestMessage:string) =
        static member (<--) (receiver:Receiver,message:string) =
            Receiver(message)
        static member (-->) (message,receiver:Receiver) =
            Receiver(message)
    let r = Receiver "no message"
    r <-- "Message One"
    "Message Two" --> r

分割操作

  • 自定义分割操作符变的更加简单
    e1.[e2opt.. e3opt]   --> e1.GetSlice(arg2,arg3)
    e1.[*]               --> e1.GetSlice(None,None)

序列和计算表达式

  • 省略执行命令时的 do 前缀
    let s =
        seq { for i in 1..12 do
              printfn "i = %A" i
              yield i+2 }
  • let 拥有与常规相同的语法
    let s2 =
        async { let rec f x = f x + 1
                return 1 }
  • 序列化表达式要有 seq 前缀
    seq {1; 2; 3}
  • -> 和 ->> 在除 seq 之外的地方声明反对

内置函数也会产生代码

改进调试

  • 增强了单步调试, 虽然这已经是 .NET 上其他编译器都具备的, 但在 F# 编译器上必须禁用 tailcalls (--optimize- notailcalls)

库:

反射

  • Microsoft.FSharp.Reflection 更趋向于 .NET 内置的反射.

引用

  • 可用的命名空间:
    Microsoft.FSharp.Quotations
    Microsoft.FSharp.Quotations.Patterns
    Microsoft.FSharp.Quotations.DerivedPatterns
    Microsoft.FSharp.Quotations.ExprShape
  • Microsoft.FSharp.Quotations.Raw 和 Microsoft.FSharp.Quotations.Typed 被移除
  • Expr<_> 变成 Expr 的子类型.
  • 值的结合 "comes for free"
    let f (x:int) = <@ 3 + x @>
  • 表达式的结合使用 %expr
    let f (expr:Expr) = <@ 3 + %expr @>
  • 原本的活动匹配在这里 Microsoft.FSharp.Quotations.Patterns, 还有些帮助类在 Microsoft.FSharp.Quotations.DerivedPatterns.
  • BindingPattern 更名为 ExprShape

事件和委托

  • IEvent 模块更名为 Event, Event.create 语法不变.
  • 创建正规的事件 new Event<args>() 或者指定委托类型 new Event<delegate,args>().
  • 触发事件 ev.Trigger(args) 或 ev.Trigger(sender,args)
  • 公开事件 ev.Publish
  • 类型 IHandlerEvent<_> 被删除.
  • 例如:
    type MyControl() =
        let tickEvent = new Event<int>()
        member self.React() =
            tickEvent.Trigger (4)
        member self.OnTick = tickEvent.Publish

已删除的特性

  • Unicode 语法
  • IEnumerable.*
  • List.of_IEnumerable List.to_IEnumerable (取而者: List.of_seq List.to_seq)
  • Permutation 类型
  • Func.*
  • CompatArray 和 CompatMatrix 模块. (取而者: Array Array2)
  • Microsoft.FSharp.Compatibility.CompatMatrix (仅 .NET1.1)
  • Microsoft.FSharp.Compatibility.CompatMatrix (仅 .NET1.1)
  • Microsoft.FSharp.Compatibility.CompatArray (仅 .NET1.1)
  • Microsoft.FSharp.Core.Idioms
  • Microsoft.FSharp.Core.Int8
  • Microsoft.FSharp.Core.Int16
  • string 现在是函数 'a -> string. 因此 string.Format 不可用, 使用 System.String.Format 代替. 同样 string.Empty 也不可用.
  • base 变成关键字
  • truncate 的类型改为 'a -> 'a (支持所有带 Truncate 静态方法的浮点型)
  • (type ty) 改为 typeof<ty> 和 typedefof<ty>
  • 抽象类必须标记 [<AbstractClass>]
  • 声明即可以解释为函数也可以解释为样式.
    let Node(x,l1,r1) = idxToNode(m1)   // 不总是函数定义

F# PowerPack

  • 用于收集 F# 的扩展和兼容功能, 未来计划在 codeplex 上开源.
  • 兼容于 Ocaml 或早期版本的组件
  • Math 组件, 包括 Matrix 和 Vector 类型
  • 度量单位定义组件
  • 一些不赞成使用的组件
  • LINQ
  • 如果你不想让程序依赖附属的库, 就必须修改不赞成使用的部分, 例如:
    Sys.argv --> System.Environment.GetCommandLineArgs()
    Char.code --> int
    Char.chr --> char
    Filename.* --> Use System.IO.Path methods instead
    Bytearray --> Array

FSharp.PowerPack.Linq.dll

  • PowerPack 支持通过 LINQ 表达式引用编辑和赋值
    open Microsoft.FSharp.Linq.QuotationEvaluation
    let q = <@ 1 + 1 @>
    q.ToLinqExpression ()   // 转换为 LINQ 表达式
    q.Compile ()   // 返回类型 'unit -> int'
    q.Eval ()   // 返回 2
  • PowerPack 支持通过 LINQ 表达式查询赋值
    open Microsoft.FSharp.Linq.Query
    type Customer = { Name:string; Data: int; Cost:float; Sizes: int list }
    let c1 = { Name="Don"; Data=6; Cost=6.2; Sizes=[1;2;3;4] }
    let c2 = { Name="Peter"; Data=7; Cost=4.2; Sizes=[10;20;30;40] }
    let c3 = { Name="Freddy"; Data=8; Cost=9.2; Sizes=[11;12;13;14] }
    let c4 = { Name="Freddi"; Data=8; Cost=1.0; Sizes=[21;22;23;24] }
    let data = [c1;c2;c3;c4]
    let db = System.Linq.Queryable.AsQueryable<Customer>(data |> List.to_seq)
    // 执行查询
    <@ seq { for i in db do
                 for j in db do
                     yield (i.Name,j.Name) } @>
Tags Tags:
Categories: 编程
Posted By: colder
Last Edit: 10 Sep 2008 @ 08 33 上午

E-mailPermalink
 

Responses to this post » (One Total)

 
  1. 匿名 said...
    17:01 - 十一月 4th, 2008

    Cool

 

Leave A Comment ...

 

 XHTML:
You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
\/ More Options ...
Change Theme...
  • Role »
  • Posts »
  • Comments »
Change Theme...
  • VoidVoid (Default)
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LiteLightweight
  • No Child Pages...