【问题标题】:F# how to extended the generic array type? [duplicate]F#如何扩展泛型数组类型? [复制]
【发布时间】:2013-08-21 22:28:27
【问题描述】:

this question 之后,我想知道如何(或是否)我可以扩展通用 F# 数组类型。 我可以这样做:

type System.Array with
    member a.Last = a.GetValue(a.Length - 1)

但正如 Tomas 所说,它是非通用的。接下来我尝试了这个,但它不起作用:

type Microsoft.FSharp.Collections.Array with  // Error: Array is not defined      
    member a.Last = a.[a.Length - 1]

在 F# 源中我找到了这个命名空间,但它也不起作用:

type Microsoft.FSharp.Primitives.Basics.Array with  // Error: Array is not defined          
    member a.Last = a.[a.Length - 1]

【问题讨论】:

    标签: f# type-extension


    【解决方案1】:

    这有点令人困惑 - 但我最近在 F# 规范中寻找一些东西并遇到了这个:

    type 'T ``[]`` with
        member a.Last = a.[a.Length - 1]
    
    [| 1 .. 10 |].Last
    

    双反引号编码通常用于将保留关键字转换为有效的 F# 标识符(例如,如果您想要一个名称中包含空格的属性,或者命名为 let)。在这里,它可能意味着编译器需要将[] 视为普通类型“名称”而不是数组的特殊语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2018-03-19
      • 2021-03-25
      相关资源
      最近更新 更多