On Sat, Jul 18, 2020 at 1:55 PM Markus Heukelom <markus.heuke...@gain.pro>
wrote:

> Concerning the current generics proposal, was it every considered to not
> allow type contracts at all?
>
> No type contracts would mean that generic functions can only use =, & on
> variables of parametric types, as these are always available for all types.
> Nothings else is allowed.
>
>
I've written a lot of code in this style in Standard ML. In fact, SML
distinguishes between types and eqtypes, where the latter allows for
equality comparisons. Many types do not have an equality check defined on
them, most notably floating point values and functions among the ground
types.

The place where you hit the limitation is usually when you have an ordered
tree and you need a function `cmp` of type `'k -> 'k -> order` which can
order the keys in the tree. When you construct the tree, you have to
provide such an ordering function and embed the order in the data structure
of the tree. It is usually nicer to have that order implicitly bound to the
type, especially for trees where you don't require a specific order, only
some order that is total (or perhaps well-ordered).

Standard ML gets around this limitation by the means of "Functors" which
are functions from modules to modules. In Go-parlance that would be
generics on the package level, which I believe has been explored. My guess
is that to make this efficient in writing, you also need to have nested
packages, something which Standard ML has, but Go doesn't. Functors are
also a nominal construction, whereas Go tends to use structural
constructions where possible.

The current Go solution is closer to Haskell, where you can write the
signature for the insertion into a tree as

insert :: Ord k => k -> a -> Map k a -> Map k a

that is, given a key, k, a value of type a, and a map for those types,
produce a map where the K/V pair has been inserted. The `Ord k => ...` part
constrain the type `k` to be ordered.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGrdgiX8EnOdq38cLwg3jsqK7-k7Vv9GKq1qXChk1VATajDfpw%40mail.gmail.com.

Reply via email to