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  
    25  type S2 struct {
    26  	Eint
    27  	Ebool
    28  	v string
    29  }
    30  
    31  type S3 struct {
    32  	*E[int]
    33  }
    34  
    35  func main() {
    36  	s1 := S1{Eint{2}, "foo"}
    37  	if got, want := s1.E.v, 2; got != want {
    38  		panic(fmt.Sprintf("got %d, want %d", got, want))
    39  	}
    40  	s2 := S2{Eint{3}, Ebool{true}, "foo"}
    41  	if got, want := s2.Eint.v, 3; got != want {
    42  		panic(fmt.Sprintf("got %d, want %d", got, want))
    43  	}
    44  	var s3 S3
    45  	s3.E = &Eint{4}
    46  	if got, want := s3.E.v, 4; got != want {
    47  		panic(fmt.Sprintf("got %d, want %d", got, want))
    48  	}
    49  }
    50  

View as plain text