Skip to content

Instantly share code, notes, and snippets.

@alphazero
Last active August 31, 2017 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphazero/4e31aa4cce94fbb8b60c7372b21a6684 to your computer and use it in GitHub Desktop.
Save alphazero/4e31aa4cce94fbb8b60c7372b21a6684 to your computer and use it in GitHub Desktop.
A refinement of the half-baked idea
/* define generic package */
// file p.go
package p
typevar K generic.ComparableType // poor man catagory
typevar V generic.AnyType // ^^
import (...)
type X struct {
k K
v V
}
func (p *X) Get() (K, V) { return p.k, p.v }
// -----------
/* use generic package - module type directive decl. can be done various way */
package main
/* possible way for having n distinct type flavors of a given gen package in same source */
import (
psb "p" //+build {K:string, V:[]byte}
pss "p" //+build {K:string, V:string}
)
func main() {
var x = psb.X {"this-thing", []byte("it is generic"),}
var k string
var v []byte
k, v = x.Get()
var x1 = pss.X {"this-thing", "it is generic",}
var k1, v1 string
k1, v1 = x1.Get()
} /* the Q is how far does this get us .. */
@alphazero
Copy link
Author

Sans the hypothetical generics package (magic package ala builtin), this is mostly the build tool chain, so yes, code generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment