Skip to content

proposal: spec: generic parameterization of array sizes #44253

Open
@ajwerner

Description

@ajwerner

See detailed design here. The recent acceptance of #43651, we can now begin discussing filling in the gaps and omissions of that proposal. This issue provides a proposal to fulfill the following stated omission in the type parameter proposal:

No parameterization on non-type values such as constants. This arises most obviously for arrays, where it might sometimes be convenient to write type Matrix[n int] [n][n]float64. It might also sometimes be useful to specify significant values for a container type, such as a default value for elements.

The structure of the proposal boils down to two extensions:

  1. Expression of all array types inside of type lists

Today one can specify a type constraint that a type is an array of some specific, constant size. For example:

type Array8[T any] interface {
    type [8]T
}

Similarly one can enumerate a set of array sizes:

type ArraysOfSomeSizes[T any] interface {
    type [2]T, [4]T, [8]T, [16]T
}

But, there's no way to capture the idea of all arrays of a given type. This proposal proposes the following syntax to capture that idea:

type Array[T any] interface {
    type [...]T
}

This syntax is familiar as it is used for length inference in array literals.

  1. Permit constant expressions to determine the length of an array type using len([...]T)

I'm not wedded in any way to the function with this behavior actually being len. It could be some
new thing like ArrayLen([...]T) in some package somewhere.

Today go provides both constant expressions which take types (see unsafe.Sizeof) and more generally
builtin functions which take types. Secondly, the go compiler already statically computes array lengths
at compile time when it can.

Constant expressions can be already be used to size arrays.

Taken together, one can imagine using this constant expression over some parameterized array type to
size other array types.

A detailed discussion with examples, background and motivation can be found in this proposal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions