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

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

内容简介: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的互操作》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

网站搜索设计

网站搜索设计

[美] Shari Thurow、[美] Nick Musica / 向怡宁 / 人民邮电出版社 / 2011-4 / 35.00

本书是提高网站搜索可用性的红宝书,它将SEO 和Web 可用性两个不同领域的知识融会贯通,详细阐述了用户的各种搜索行为和行为背后的真实意图,以及网站如何迎合用户心理,以便提供令其满意的内容,进而实现网站所有者的商业目标。 本书不仅仅是SEO 专业人员和Web 可用性人员的参考必备,同时更可为网络文案、设计开发人员、营销专员以及网站所有者、管理者等其他Web 领域从业人员拓展视野、补强技能。一起来看看 《网站搜索设计》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

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

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具