Skip to content

Type syntax for 5d, 6d etc. arrays #12013

@dsyme

Description

@dsyme

DiffSharp has need for 5D and 6D array types, e.g. types int[,,,,] and int[,,,,,].

These are actually allowed in F# through the syntax using explicit double back ticks:

int ``[,,,,]``

However the syntax int[,,,,] should be supported directly

Note that this is worth having even if we don't have Array4D and Array5D modules in FSharp.Core, as you can then write a module of operations like this:

module Array5D =
    let zeroCreate<'T> (length1:int) length2 length3 length4 length5 : 'T ``[,,,,]`` =
        let result = System.Array.CreateInstance(typeof<'T>, [|length1;length2;length3;length4;length5|])
        result :?> 'T ``[,,,,]``

    let get (array:'T ``[,,,,]``) (index1:int) index2 index3 index4 index5 : 'T =
        let result = array.GetValue([|index1;index2;index3;index4;index5|])
        result :?> 'T

    let set (array:'T ``[,,,,]``) (index1:int) index2 index3 index4 index5 (value: 'T) =
        array.SetValue(box value, [|index1;index2;index3;index4;index5|])
   
    let length1 (array:'T ``[,,,,]``) = array.GetLength(0)
    let length2 (array:'T ``[,,,,]``) = array.GetLength(1)
    let length3 (array:'T ``[,,,,]``) = array.GetLength(2)
    let length4 (array:'T ``[,,,,]``) = array.GetLength(3)
    let length5 (array:'T ``[,,,,]``) = array.GetLength(4)

    let init<'T> (length1:int) length2 length3 length4 length5 (initializer:int->int->int->int->int->'T) : 'T ``[,,,,]`` =
        let arr = zeroCreate<'T> length1 length2 length3 length4 length5
        for i1=0 to length1-1 do
            for i2=0 to length2-1 do
                for i3=0 to length3-1 do
                    for i4=0 to length4-1 do
                        for i5=0 to length5-1 do
                            set arr i1 i2 i3 i4 i5 (initializer i1 i2 i3 i4 i5)
        arr

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions