Source file test/typeparam/issue49027.dir/a.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package a
     6  
     7  func Conv(v interface{}) string {
     8  	return conv[string](v)
     9  }
    10  
    11  func conv[T any](v interface{}) T {
    12  	return v.(T)
    13  }
    14  
    15  func Conv2(v interface{}) (string, bool) {
    16  	return conv2[string](v)
    17  }
    18  
    19  func conv2[T any](v interface{}) (T, bool) {
    20  	x, ok := v.(T)
    21  	return x, ok
    22  }
    23  
    24  func Conv3(v interface{}) string {
    25  	return conv3[string](v)
    26  }
    27  
    28  func conv3[T any](v interface{}) T {
    29  	switch v := v.(type) {
    30  	case T:
    31  		return v
    32  	default:
    33  		var z T
    34  		return z
    35  	}
    36  }
    37  
    38  type Mystring string
    39  
    40  func (Mystring) Foo() {
    41  }
    42  
    43  func Conv4(v interface{Foo()}) Mystring {
    44  	return conv4[Mystring](v)
    45  }
    46  
    47  func conv4[T interface{Foo()}](v interface{Foo()}) T {
    48  	switch v := v.(type) {
    49  	case T:
    50  		return v
    51  	default:
    52  		var z T
    53  		return z
    54  	}
    55  }
    56  

View as plain text