F# how to extended the generic array type? [duplicate]

痴心易碎 提交于 2019-12-04 03:24:22

问题


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)

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]

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]

回答1:


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

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.



来源:https://stackoverflow.com/questions/18359825/f-how-to-extended-the-generic-array-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!