blob: 01590585c53d6b3d7a1b97783edbf15684c8b204 [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Rob Pikea65cc2a2008-06-18 11:51:21 -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
Russ Cox839a6842009-01-20 14:40:40 -08009type Element interface {
Rob Pikea65cc2a2008-06-18 11:51:21 -070010}
11
Russ Cox839a6842009-01-20 14:40:40 -080012type Vector struct {
Russ Coxd47d8882008-12-18 22:37:22 -080013 elem []Element;
Rob Pikea65cc2a2008-06-18 11:51:21 -070014}
15
16func (v *Vector) At(i int) Element {
17 return v.elem[i];
18}
19
Russ Cox839a6842009-01-20 14:40:40 -080020type TStruct struct {
Rob Pikea65cc2a2008-06-18 11:51:21 -070021 name string;
22 fields *Vector;
23}
24
Rob Pikec137d6b2008-06-25 15:22:27 -070025func (s *TStruct) field(i int) *TStruct {
Russ Cox49e20872009-02-11 17:55:16 -080026 return s.fields.At(i).(*TStruct);
Rob Pikec137d6b2008-06-25 15:22:27 -070027}
28
29func main() {
Russ Cox55645042009-01-06 15:19:02 -080030 v := new(Vector);
31 v.elem = make([]Element, 10);
32 t := new(TStruct);
Rob Pikec137d6b2008-06-25 15:22:27 -070033 t.name = "hi";
34 v.elem[0] = t;
Russ Cox55645042009-01-06 15:19:02 -080035 s := new(TStruct);
Rob Pikec137d6b2008-06-25 15:22:27 -070036 s.name = "foo";
37 s.fields = v;
38 if s.field(0).name != "hi" {
Rob Pikebc2f5f12008-08-11 22:07:49 -070039 panic("bad name")
Rob Pikec137d6b2008-06-25 15:22:27 -070040 }
Rob Pikea65cc2a2008-06-18 11:51:21 -070041}