F#如何扩展通用数组类型? [英] F# how to extended the generic array type?

查看:85
本文介绍了F#如何扩展通用数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之后我想知道如何(或是否)可以扩展通用F#数组类型. 我可以这样做:

Following this question I wonder how (or if) i can extend the generic F# Array Type. I could do this:

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

但是正如Tomas所提到的那样,它是非通用的.接下来,我尝试了此操作,但它不起作用:

but as Tomas mentioned it is non generic. Next I tried this but it does not work:

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

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

In the F# scource I found this namespace, but it does not work either:

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

推荐答案

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

This is a bit confusing - but I was recently looking for something in the F# specification and came across this:

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

[| 1 .. 10 |].Last

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

The double-backtick encoding is normally used to turn reserved keywords into valid F# identifiers (e.g. if you want to have a property that has a space in the name, or is named let). Here, it probably means that the compiler needs to treat [] as an ordinary type "name" rather than as a special syntax for arrays.

这篇关于F#如何扩展通用数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆