summaryrefslogtreecommitdiffstats
path: root/llgo/test/execution/switch/type.go
blob: b43231cd935e2a4269dddc9539344be683ad15f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// RUN: llgo -o %t %s
// RUN: %t 2>&1 | FileCheck %s

// CHECK: int64 123
// CHECK-NEXT: default
// CHECK-NEXT: uint8 or int8
// CHECK-NEXT: uint8 or int8
// CHECK-NEXT: N

package main

func test(i interface{}) {
	switch x := i.(type) {
	case int64:
		println("int64", x)
	// FIXME
	//case string:
	//	println("string", x)
	default:
		println("default")
	}
}

type stringer interface {
	String() string
}

func printany(i interface{}) {
	switch v := i.(type) {
	case nil:
		print("nil", v)
	case stringer:
		print(v.String())
	case error:
		print(v.Error())
	case int:
		print(v)
	case string:
		print(v)
	}
}

func multi(i interface{}) {
	switch i.(type) {
	case uint8, int8:
		println("uint8 or int8")
	default:
		println("something else")
	}
}

type N int

func (n N) String() string { return "N" }

func named() {
	var x interface{} = N(123)
	switch x := x.(type) {
	case N:
		// Test for bug: previously, type switch was
		// assigning underlying type of N (int).
		println(x.String())
	}
}

func main() {
	test(int64(123))
	test("abc")
	multi(uint8(123))
	multi(int8(123))
	named()
}
OpenPOWER on IntegriCloud