Skip to content

spec: allow eliding interface{ } in constraint literals #48424

Closed
@fzipp

Description

@fzipp

I propose an additional rule for constraints in type parameter lists:

[T nonInterfaceType][T interface{~nonInterfaceType}]

Rationale:

All functions of the proposed maps package (#47649) and most functions of the slices package (#45955, #47203) currently use constraints from the constraints package (#45458) for maximum generality. A hypothetical chans package would probably be similar (there's already a proposal for constraints.{ReadOnlyChan|WriteOnlyChan}: #48366). Some excerpts:

func EqualFunc[M1 constraints.Map[K, V1], M2 constraints.Map[K, V2], K comparable, V1, V2 any](m1 M1, m2 M2, cmp func(V1, V2) bool) bool

func Clone[S constraints.Slice[T], T any](s S) S

With this proposal these would become:

func EqualFunc[M1 map[K]V1, M2 map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, cmp func(V1, V2) bool) bool

func Clone[S []T, T any](s S) S

The benefit:

  • constraints.{Slice|Map|Chan|ReadOnlyChan|WriteOnlyChan} would not be necessary at all.
  • No visual incongruency between plain old Go types like map[K]V, []T, chan T, <-chan T, chan<- T and their accompanying constraints.

This proposal stems from the discussion under #47330 (reply in thread)

Summary:

  [M map[K]V, K comparable, V any]
≡ [M interface{~map[K]V}, K comparable, V any]
≡ [M constraints.Map[K, V], K comparable, V any]

  [S []T, T any]
≡ [S interface{~[]T}, T any]
≡ [S constraints.Slice[T], T any]

  [C <-chan T, T any]
≡ [C interface{~<-chan T}, T any]
≡ [C constraints.ReadOnlyChan[T], T any]

  [C chan<- T, T any]
≡ [C interface{~chan<- T}, T any]
≡ [C constraints.WriteOnlyChan[T], T any]

  [C chan T, T any]
≡ [C interface{~chan T}, T any]
≡ [C constraints.Chan[T], T any]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions