Skip to content

Instantly share code, notes, and snippets.

@PeterRK
Last active October 16, 2018 03:59
Show Gist options
  • 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.

@kirillx
Copy link

kirillx commented Sep 14, 2018

Support the idea of package-level generics.
It keeps the code clean and simple, unlike to C++ generics where you have to type in plenty of places making code a mess.
It also allows to keep call traces readable, by adding types to file names, rather then functions (which are long lines already).

@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