Created
March 28, 2012 13:13
-
-
Save alecthomas/2226031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TaskNode struct { | |
children []*Node | |
} | |
func (self *Task) AddChild(node *TaskNode) { | |
self.children = append(self.children, node) | |
} | |
func (self *Task) ChildAt(index int) *TaskNode { | |
return self.children[index] | |
} | |
// There are other functions on *Task, omitted for brevity | |
// TaskList should behave like a TaskNode, but with extra functionality | |
type TaskList struct { | |
*TaskNode | |
title string | |
} | |
func (self *TaskList) SetTitle(title string) { | |
self.title = title | |
} | |
type Task struct { | |
*TaskNode | |
priority int | |
} | |
func (self *Task) GetPriority() int { | |
return self.priority | |
} | |
// So now the following should work... | |
list := NewTaskList() | |
list.SetTitle("a list") | |
task := NewTask() | |
task.SetPriority(10) | |
list.AddChild(task) | |
// How can I cast this to *Task? | |
taskRef := list.ChildAt(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment