Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Test types of constant expressions, using reflect. |
| 8 | |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
Russ Cox | 1af3edc | 2009-07-03 09:45:15 -0700 | [diff] [blame] | 11 | import "reflect" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 12 | |
Russ Cox | 07abf1c | 2011-04-25 13:39:36 -0400 | [diff] [blame] | 13 | func typeof(x interface{}) string { return reflect.TypeOf(x).String() } |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 14 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 15 | func f() int { return 0 } |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 16 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 17 | func g() int { return 0 } |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 18 | |
| 19 | type T func() int |
| 20 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 21 | var m = map[string]T{"f": f} |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 22 | |
| 23 | type A int |
| 24 | type B int |
| 25 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 26 | var a A = 1 |
| 27 | var b B = 2 |
| 28 | var x int |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 29 | |
| 30 | func main() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 31 | want := typeof(g) |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 32 | if t := typeof(f); t != want { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 33 | println("type of f is", t, "want", want) |
| 34 | panic("fail") |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 37 | want = typeof(a) |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 38 | if t := typeof(+a); t != want { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 39 | println("type of +a is", t, "want", want) |
| 40 | panic("fail") |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 41 | } |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 42 | if t := typeof(a + 0); t != want { |
| 43 | println("type of a+0 is", t, "want", want) |
| 44 | panic("fail") |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 45 | } |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 46 | } |