Created
April 14, 2012 16:36
-
-
Save alphazero/2385679 to your computer and use it in GitHub Desktop.
sketch of an approach for Go generics
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
// {}, <>, //, ||, whatever is easy on the eye and the compiler | |
type{T} Heap interface { | |
sort.Interface | |
Push(T) // phase 1 of compile, mark t as `interface{T}` | |
Pop() T | |
} | |
// limit definition to interface and function type. | |
// as the main issue is call site not internals | |
// | |
// e.g. this is probably not even necessary | |
type Int64Heap Heap{int64} | |
// | |
// but this is - generic function type is a must | |
type{T} Unmarshal func(a interface{}) T | |
// | |
// and o/c we would need generic function definitions | |
func{T} Push(h Heap{T}, item T) { ... } | |
func{T} Pop(h Heap{T}) T { ... } | |
// T of Heap{T} can be inferred | |
func{T} Push(h Heap, item T) { ... } | |
func{T} Pop(h Heap) T { ... } | |
// partial obj files in $GOPATH/pkg/$GOARCH/_generics/{<package-path>}/heap.gx | |
// but only elements that are generic. The rest can go into usual heap.a | |
// so bloat is limited only to the indeterminate bits | |
// or something like that | |
// - - - - - - | |
// elsewhere in Heap user's package foo/bar | |
// comp of this could generate finalized code in foo/bar | |
// First create heap#foo_bar.a or just append to heap.a | |
// then link to it. | |
type Item struct { ... } | |
type Queue []*Item | |
// Queue supports Heap{Item} | |
func{Item} (q Queue) Push(item Item) { ...} | |
func usage{} { | |
h := make(Queue, ...) | |
var h1 Heap{Item} = h // everyone's happy | |
var h2 Heap = h // & why not? | |
h.Push(newItem) | |
var i Item = h.Pop() | |
} | |
// - - - - - - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment