Question

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]
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top