F# 4.7 已经发布,该版本重点是编译器和核心库的基础结构更改,首先需要安装:
- 最新版 .NET Core 3.0
- 最新版 Visual Studio
如果你是 Visual Studio 用户,默认情况下将会获取到 .NET Core。一旦安装了 .NET Core 或 Visual Studio 2019,就可以在 Visual Studio、Visual Studio for Mac 或 Visual Studio Code 中使用 F# 4.7。具体更新内容如下:
FSharp.Core 支持 .NET Standard 2.0
从 FSharp.Core 4.7.0 和 F# 4.7 开始,放弃对 .NET Standard 1.6 的支持。FSharp.Core 开始支持 .NET Standard 2.0,而在 .NET Core 上具有的特性:
- 依赖关系更加简单(特别是如果使用 Paket 这样的工具)
- 在 FSharpFunc<'T, 'TResult> 上的 FromConverter 和 ToConverter 静态方法
- FSharpFunc<'T, 'TResult> 和 Converter<'T, 'TResult> 之间的隐式转换
- FuncConvert.ToFSharpFunc<'T> 方法
- 访问 MatchFailureException 类型
- 以 F# 方式更友好地使用旧 Web API 的 WebExtenations 命名空间
另外,FSharp.Core API 扩展到更好地支持并行和顺序异步计算:
- Async.Parallel 有一个可选的 maxDegreesOfParallelism 参数,以调整所使用的并行度
- Async.Sequential 允许顺序处理异步计算
支持 LangVersion
F# 4.7 引入了使用编译器调整有效语言版本的能力,它允许我们为任何给定的编译器版本提供预览功能和发布功能。其中预览功能,只需在项目文件中设置以下属性:
<!-- Example -->
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
保存项目文件后,编译器将允许你访问该编译器附带的所有预览功能。在 .NET Core 和 Visual Studio 预览版本中使用 F# 时,默认情况下,语言版本将设置为预览,支持最低的语言版本是 F# 4.6。
Implicit yields
F# 4.7 为列表、数组、序列和定义 Yield、Combine、Delay 和 Zero 成员的任何计算表达式引入 implicit yield。使用 F# 过程中,需要在 F# 序列表达式中指定 yield 关键字,此版本可以删除所有 yield 关键字,因为它们是隐式的。
// Broke: F# 4.6 and lower
let nums =
seq {
yield 1
yield 2
yield 3
}
// Woke and Bespoke: F# 4.7 and higher
let nums' =
seq {
1
2
3
}
这使得 F# 序列表达式与 List 和数组表达式对齐。在 F# 4.7 之前,即使使用列表和数组,如果要有条件地生成值,则需要在任何地方指定 yield,现在所有的 yield 关键字可以删除:
// Broke: F# 4.6 and lower
let f includeWeekend =
[
yield "Monday"
yield "Tuesday"
yield "Wednesday"
yield "Thursday"
yield "Friday"
if includeWeekend then
yield "Saturday"
yield "Sunday"
]
// Woke and bespoke: F# 4.7 and higher
let f' includeWeekend =
[
"Monday"
"Tuesday"
"Wednesday"
"Thursday"
"Friday"
if includeWeekend then
"Saturday"
"Sunday"
]
不再需要双下划线
在 F# 4.7 之前,如果想指定成员声明,并且不想在 F# 对象上命名‘this’标识符,则必须使用双下划线。此版本,只需使用一个下划线:
// Broke: F# 4.6 and lower
type C() =
member __.M() = ()
// Woke and Bespoke: F# 4.7 and higher
type C'() =
member _.M() = ()
传递给构造函数和静态方法参数的缩进
以前的 F# 编译器需要对构造函数或静态方法缩进参数,这是一个缺陷,此版本做了改进:
// Broke: F# 4.6 and lower
type OffsideCheck(a:int,
b:int, c:int,
d:int) = class end
type C() =
static member M(a:int,
b:int, c:int,
d:int) = 1
// Woke and Bespoke: F# 4.7 and higher
type OffsideCheck'(a:int,
b:int, c:int,
d:int) = class end
type C'() =
static member M(a:int,
b:int, c:int,
d:int) = 1
预览功能
包含了两个新的预览功能:nameof 和开放静态类。详细介绍见发布说明。
F# Interactive for .NET Core Preview
从 F# 4.7 和 .NET Core 3 开始,可以使用 .NET Core 中的 F# Interactive (FSI),只需打开命令行并键入 dotnet fsi 即可开始。
对 Visual Studio 的 F# 工具的更新
Visual Studio 2019 16.3 版本与 F# 4.7 和 .NET Core 3 相对应。在这个版本中,工具提示(tooltip)得到改进,并修复了编译器和工具中影响用户在 Visual Studio 中体验的一些长期存在的问题。还有 F# 与 Roslyn 的集成更加稳定。
Record 定义工具提示(tooltip)使用更规范的格式:
匿名 Record 也一样:
FSI 中的 record 值输出也使用了更规范的形式:
显式 GET/SET 修饰符的属性还将在工具提示中反映这些修饰符:
更多内容见发布说明: