F# 4.1提供改善,并支持与C# 7的互操作

栏目: ASP.NET · 发布时间: 8年前

内容简介:F# 4.1提供改善,并支持与C# 7的互操作

F# 4.1 对语言进行了很多改进。F# 4.1将通过新版本的Microsoft tools for F#提供,Microsoft tools for F#据说将于今年晚些时候发布。该版本支持结构体元组(struct tuples),与C# 7的互操作,以及by-ref返回。

由于F#的语法和类型推断简化了元组的使用,元组通常在F#中使用。它们是存储在堆栈上的引用类型。F# 4.1中提供了存储在堆栈上的结构体元组。对于某些场景来说,性能得到了提升,比如说需要分配大量的小元组。

要支持ValueTuple类型,元组类型、元组表达式和元组模式可以用关键字struct来注释。

// Creating a new struct tuple.
let origin = struct (0, 0)

// Take struct tuples as arguments to a function and generate a new struct tuple.
let getPointFromOffset (point: struct (x, y)) (offset: struct (dx, dy)) = 
    struct (x + dx, y + dy)

// Pattern match on a struct tuple.
let doAMatch (input: struct (x, y)) =
    match input with
    | struct (0, 0) -> sprintf "The tuple is the origin!"
    | struct (_, _) -> sprintf "The tuple is NOT the origin!"

与C# 7的互操作也支持使用struct关键字。

// Calls a C# function returning a value tuple
let struct(word, value) = SomeService.SomeResult()
// Calls a C# function taking a value tuple in parameter.
let result = SomeService.CreateResult(struct("hello", 12))

除了元组之外,记录类型和可区分联合也可以表示为值类型。它需要struct注释。

[<Struct>]
 type Student = {
    Id: int
    Name: string
    Age: int
    GPA: double
    Major: string
}

[<Struct>]
type Shape = 
| Circle of radius: float
| Square of side: int
| Rectangle of sides: double*double

F# 4.1也提供 by-ref返回 。F#已经支持了ref locals,但是不支持使用或生成byref-returning方法。

// Returns from an array.
let f (x:int[]) = &x.[0]

// Returns from a record
[<Struct>]
type R = { mutable z : int }
let f (x:byref<R>) = &x.z

by-ref返回也可以在C#的方法中使用。

public static ref int Find(int val, int[] vals)
{
    for (int i = 0; i < vals.Length; i++)
    {
        if (vals[i] == val)
        {
            return ref numbers[i];
        }
    }
}

// 'result' is of type 'byref<int>'.
let result = SomeCSharpClass.Find(3, [| 1; 2; 3; 4; 5 |])

除了GitHub上公布的F#源代码,你也可以参考公开的 语言规范 获取更多信息。

查看英文原文: F# 4.1 Brings Improvements and Interoperation with C# 7


以上所述就是小编给大家介绍的《F# 4.1提供改善,并支持与C# 7的互操作》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax基础教程

Ajax基础教程

(美)阿斯利森、(美)舒塔、金灵 / 金灵 / 人民邮电出版社 / 2006-02-01 / 35.00元

Ajax技术可以提供高度交互的Web应用,给予用户更丰富的页面浏览体验。本书重点介绍Ajax及相关的工具和技术,主要内容包括XMLHttpRequest对象及其属性和方法、发送请求和处理响应、构建完备的Ajax开发工具、使用JsUnit测试JavaScript、分析JavaScript调试工具和技术,以及Ajax开发模式和框架等。本书中所有例子的代码都可以从Apress网站本书主页的源代码(Sou......一起来看看 《Ajax基础教程》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具