blob: 874b47e7adc3f4ab88ef8127af72e5c04479e99f [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Rob Pike094ee442008-06-06 16:56:18 -07002
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
7package main
8
Ian Lance Taylorf2030932012-01-18 14:31:31 -08009import "fmt"
10
Russ Cox839a6842009-01-20 14:40:40 -080011type Element interface {
Rob Pike094ee442008-06-06 16:56:18 -070012}
13
Russ Cox839a6842009-01-20 14:40:40 -080014type Vector struct {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080015 nelem int
16 elem []Element
Rob Pike094ee442008-06-06 16:56:18 -070017}
18
Russ Cox839a6842009-01-20 14:40:40 -080019func New() *Vector {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080020 v := new(Vector)
21 v.nelem = 0
22 v.elem = make([]Element, 10)
23 return v
Rob Pike094ee442008-06-06 16:56:18 -070024}
25
26func (v *Vector) At(i int) Element {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080027 return v.elem[i]
Rob Pike094ee442008-06-06 16:56:18 -070028}
29
30func (v *Vector) Insert(e Element) {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080031 v.elem[v.nelem] = e
32 v.nelem++
Rob Pike094ee442008-06-06 16:56:18 -070033}
34
Rob Pike094ee442008-06-06 16:56:18 -070035func main() {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080036 type I struct{ val int }
37 i0 := new(I)
38 i0.val = 0
39 i1 := new(I)
40 i1.val = 11
41 i2 := new(I)
42 i2.val = 222
43 i3 := new(I)
44 i3.val = 3333
45 i4 := new(I)
46 i4.val = 44444
47 v := New()
Ian Lance Taylorf2030932012-01-18 14:31:31 -080048 r := "hi\n"
Ian Lance Taylor6b346282012-01-18 13:20:55 -080049 v.Insert(i4)
50 v.Insert(i3)
51 v.Insert(i2)
52 v.Insert(i1)
53 v.Insert(i0)
Rob Pike094ee442008-06-06 16:56:18 -070054 for i := 0; i < v.nelem; i++ {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080055 var x *I
56 x = v.At(i).(*I)
Ian Lance Taylorf2030932012-01-18 14:31:31 -080057 r += fmt.Sprintln(i, x.val) // prints correct list
Rob Pike094ee442008-06-06 16:56:18 -070058 }
59 for i := 0; i < v.nelem; i++ {
Ian Lance Taylorf2030932012-01-18 14:31:31 -080060 r += fmt.Sprintln(i, v.At(i).(*I).val)
61 }
62 expect := `hi
630 44444
641 3333
652 222
663 11
674 0
680 44444
691 3333
702 222
713 11
724 0
73`
74 if r != expect {
75 panic(r)
Rob Pike094ee442008-06-06 16:56:18 -070076 }
77}
Ian Lance Taylor6b346282012-01-18 13:20:55 -080078
Rob Pike094ee442008-06-06 16:56:18 -070079/*
80bug027.go:50: illegal types for operand
81 (<Element>I{}) CONV (<I>{})
82bug027.go:50: illegal types for operand
83 (<Element>I{}) CONV (<I>{})
84*/