Source file test/typeparam/struct.go

     1  // run
     2  
     3  // Copyright 2021 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  package main
     8  
     9  import (
    10  	"fmt"
    11  )
    12  
    13  type E[T any] struct {
    14  	v T
    15  }
    16  
    17  type S1 struct {
    18  	E[int]
    19  	v string
    20  }
    21  
    22  type Eint = E[int]
    23  type Ebool = E[bool]
    24  type Eint2 = Eint
    25  
    26  type S2 struct {
    27  	Eint
    28  	Ebool
    29  	v string
    30  }
    31  
    32  type S3 struct {
    33  	*E[int]
    34  }
    35  
    36  func main() {
    37  	s1 := S1{Eint{2}, "foo"}
    38  	if got, want := s1.E.v, 2; got != want {
    39  		panic(fmt.Sprintf("got %d, want %d", got, want))
    40  	}
    41  	s2 := S2{Eint{3}, Ebool{true}, "foo"}
    42  	if got, want := s2.Eint.v, 3; got != want {
    43  		panic(fmt.Sprintf("got %d, want %d", got, want))
    44  	}
    45  	var s3 S3
    46  	s3.E = &Eint{4}
    47  	if got, want := s3.E.v, 4; got != want {
    48  		panic(fmt.Sprintf("got %d, want %d", got, want))
    49  	}
    50  }
    51  

View as plain text