//===========================
package composite
import "container/vector"
type Node interface {
Size() int
Name() string
}
//-----------------------
type File struct {
name string
size int
}
func NewFile(name string, size int) *File {
if (size < 0 || name == "") {
return nil
}
return &File{name:name, size:size}
}
func (file *File) Name() string {
return file.name
}
func (file *File) Size() int {
return file.size
}
//-----------------------
type Directory struct {
name string
children *vector.Vector
}
func NewDirectory (name string) *Directory {
if (name == "") {
return nil
}
return &Directory{name:name, children:new(vector.Vector)}
}
func (dir *Directory) Name() string {
return dir.name
}
func (dir *Directory) Size() int {
size := 0
for i := 0; i < dir.children.Len(); i++ {
node := dir.children.At(i).(*Node)
size += node.Size()
}
return size
}
func (dir *Directory) Add(node *Node) {
dir.children.Push(node)
}
//===========================
package main
import "composite"
func main() {
var dir = composite.NewDirectory("dir1")
var file = composite.NewFile("file1", 20)
dir.Add(file)
}
But if I try add a File object to Directory the compiler complains:
cannot use file (type *composite.File) as type *composite.Node in
function argument
I'd appreciate if someone could help me. How do I have to write the Add
() function in Directory to accept Files and Directories (or more
general everything that implements the Node interface)?
Thanks!
Frank
> func (dir *Directory) Size() int {
> size := 0
> for i := 0; i < dir.children.Len(); i++ {
> node := dir.children.At(i).(*Node)
> size += node.Size()
> }
> return size
> }
>
> func (dir *Directory) Add(node *Node) {
> dir.children.Push(node)
> }
Node is an interface value. The type *File satisfies the interface
Node. So if your function has a parameter of type Node, you can pass
an argument of type *File.
You've written your function with a parameter of *Node, which is not
an interface--it's a pointer to an interface. You can only pass an
argument of type *Node.
Similarly, once you push a Node, in Add, you will want to pull out a
Node, not a *Node, in Size.
Ian