Skip to content

Instantly share code, notes, and snippets.

@PeterRK
Last active October 16, 2018 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterRK/41d4d3f54b8db55cd616403fd5a389f3 to your computer and use it in GitHub Desktop.
Save PeterRK/41d4d3f54b8db55cd616403fd5a389f3 to your computer and use it in GitHub Desktop.
package level generics

Package level generics may look less confusing.

package abc(Key,Value)

contract CompileCheck(a1 Key, a2 Value) {
	//...
}

type Pair struct {
	K Key
	V Value
}

func NewPair(k Key, v Value) Pair {
	return Pair{K: k, V: v}
}
package xyz

import "abc(int32,float32)" myabc

func newPair() myabc.Pair {
	return myabc.NewPair(1, 1.0)
}

Introduce two step compilation. Firstly create abc[Key,Value].lib from source. Then create abc(int32,float32).a from abc[Key,Value].lib . Both abc[Key,Value].lib and abc(int32,float32).a can be saved to reduce next compilation time.

@PeterRK
Copy link
Author

PeterRK commented Oct 16, 2018

Hi, I've got some comment about your idea.

Why not directly

package abc type (Key, Value)

I tried to make it impossible that define both abc(K,V) and abc(X,Y).
You are right. It's a better to let the compiler show an error in such cases.

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