Skip to content

Instantly share code, notes, and snippets.

View pat42smith's full-sized avatar

Patrick Smith pat42smith

View GitHub Profile
@pat42smith
pat42smith / go_generics_adaptors.md
Last active October 16, 2018 23:58
Go Generics with Adaptors

Go Generics with Adaptors

Contracts — Draft Design presents a draft proposal for adding generic types and functions to the Go programming language, using contracts to specify the behavior required of type parameters.

This document discusses two topics.

First, suppose generic types and functions are added to Go as in the contracts proposal, but without contracts. When a generic function f has a type parameter T, the only operations f can perform on values of type T are the operations that are available on all types. Is there a reasonable way to write generic code in these circumstances, with no other additions to the language? The answer seems to be

  • Yes, this is quite doable.
  • And it works well with both built-in and user-defined types.
@pat42smith
pat42smith / go_generics_builtin_user.md
Last active September 26, 2018 17:29
Go Generics for built-in and user-defined type parameters

Go generics for built-in and user-defined type parameters

Contracts — Draft Design presents a draft proposal for adding generic types and functions to the Go programming language, using contracts to specify the behavior required of type parameters.

The current document sketches a simple extension to that proposal, which would permit writing generic functions whose type parameters could be either built-in types or user-defined types deliberately designed to be similar to the built-in types.

Background

Consider a generic function, say