Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]
like image 377
Goswin Rothenthal Avatar asked Aug 21 '13 14:08

Goswin Rothenthal


1 Answers

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.

like image 192
Tomas Petricek Avatar answered Sep 28 '22 11:09

Tomas Petricek