Source file test/typeparam/typeswitch5.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  type myint int
    10  func (x myint) foo() int {return int(x)}
    11  
    12  type myfloat float64
    13  func (x myfloat) foo() float64 {return float64(x) }
    14  
    15  func f[T any](i interface{}) {
    16  	switch x := i.(type) {
    17  	case interface { foo() T }:
    18  		println("fooer", x.foo())
    19  	default:
    20  		println("other")
    21  	}
    22  }
    23  func main() {
    24  	f[int](myint(6))
    25  	f[int](myfloat(7))
    26  	f[float64](myint(8))
    27  	f[float64](myfloat(9))
    28  }
    29  

View as plain text