Disclaimer: I don't work on languages. I just don't like the idea of
writing contracts.
Instead of specifying the minimal set of operations a type should
satisfy, why not describe what the type should look like:
func f(in like T)
This means 'in' should be a type that has all the operations defined
on T. T can be a primitive type, an interface, a derived type, or a
struct. That way, you can write interfaces or structs to represent
contracts.
There is the problem of representing return types:
func f(in like T) (out like X)
Something like this cannot be used as initializer or on the right-side
of :=. But it can be used for something like:
var x int
x=f(5)
where return type is an int.
With this, you can write:
func Sum(in...like int) like int
and this would work for all numeric types that support the same
operations as int.
Extending the idea, you can write:
type T template {
f1(like int)
f2(like string)
}
and then use
func f(in like T)
where f is a function that operates on any type that implements an
interface like T.
Here, 'template' is a new keyword that defines a template for interfaces.
Arrays could also be represented with
func f(in []like int)
meaning the function takes an array of things that are like int.
I have to admit I did not think this through, so there are probably
many cases where this won't work. However, it looked like a better
alternative than writing contracts.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.