Source file test/typeparam/smoketest.go

     1  // compile
     2  
     3  // Copyright 2020 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // This file checks simple code using type parameters.
     8  
     9  package smoketest
    10  
    11  // type parameters for functions
    12  func f1[P any]() {}
    13  func f2[P1, P2 any, P3 any]() {}
    14  func f3[P interface{}](x P, y T1[int]) {}
    15  
    16  // function instantiations
    17  var _ = f1[int]
    18  var _ = f2[int, string, struct{}]
    19  var _ = f3[bool]
    20  
    21  // type parameters for types
    22  type T1[P any] struct{}
    23  type T2[P1, P2 any, P3 any] struct{}
    24  type T3[P interface{}] interface{}
    25  
    26  // type instantiations
    27  type _ T1[int]
    28  type _ T2[int, string, struct{}]
    29  type _ T3[bool]
    30  
    31  // methods
    32  func (T1[P]) m1()           {}
    33  func (T1[_]) m2()           {}
    34  func (x T2[P1, P2, P3]) m() {}
    35  
    36  // type lists
    37  type _ interface {
    38  	m1()
    39  	m2()
    40  	int | float32 | string
    41  	m3()
    42  }
    43  
    44  // embedded instantiated types
    45  type _ struct {
    46  	f1, f2 int
    47  	T1[int]
    48  	T2[int, string, struct{}]
    49  	T3[bool]
    50  }
    51  
    52  type _ interface {
    53  	m1()
    54  	m2()
    55  	T3[bool]
    56  }
    57  

View as plain text