Question

I have read the question F# how to extended the generic array type? and it's answer, and it's pretty close to what I wan't to do. Is it possible to extend the type 'T[][], just like it is with 'T[]?

Was it helpful?

Solution

The jagged array [][] is an array[] specialized in another array.

You can use [,] which is a 2D array:

type 'T ``[,]`` with
    member a.First = a.GetValue(0,0)

(array2D [| [| 1 .. 10 |] ; [| 11 .. 20 |] |] ).First

But if you really want to use [][] you can't do it with this F# specific notation, it is not possible to declare it specialized since this notation mirrors the Type Definition, so you have to do it the .NET way:

[<System.Runtime.CompilerServices.Extension>]
module Extensions =

    [<System.Runtime.CompilerServices.Extension>]   
    let First   (x  :_ [][]) = x.[0].[0]

It will work when accessed from C#, but from F# I think you will need F# 3.1

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