5 ms·
This is because interfaces and contracts are conceptually similar. They both give you a way to abstract over a concrete implementation of some set of operations
by munificent 7y ago
This is because interfaces and contracts are conceptually similar. They both give you a way to abstract over a concrete implementation of some set of operations. That in turn lets you use multiple different types that support those operations.
However, they operate at different times. Interfaces are a runtime abstraction and contracts are at compile time. Interfaces are dynamic and contracts are static. This manifests itself in important ways:
* Contracts give you a way to enforce homogeneity where interfaces permit heterogeneity. Say you want a function that takes a list of objects that you can call `Foo()` on. If you define that function as accepting a list of some Fooer interface, then the list you get at runtime may contain a mixture of all sorts of different types of objects, all of which implement Fooer. If your function excepts a list of a generic type implementing a Fooer contract then at runtime you get a list of objects all of the same type, one which supports supports Fooer.
* Because of the above, there is a runtime overhead to working with interfaces. When you pass a concrete type to something expecting in interface, that value needs to be boxed. When invoke a method on it, you need some amount of dynamic or virtual dispatch at runtime. With contracts, you can generate code at compile time that calls directly into the chosen type because you know at compile time exactly which concrete type has been chosen.
So I think you're right to point out the similarities. But I think having both and having them be similar is a good thing. It lowers the cognitive load while giving more things users can express. I don't think the features will compete any more than interfaces and generics compete in other languages.
There are analogues of this elsewhere. C++ basically recapitulated this same history decades ago. Initially it only had virtual methods but no generics. You would see collection libraries that tried to define collections of arbitrary types using interfaces, but it never quite worked right. A list of ints isn't really a subclass of List<T> even though there is some polymorphism going on. Templates resolved that.
Likewise, Java added generics because interfaces were not sufficiently expressive.
Rust has traits and trait objects. The two features are very similar and reuse a lot of mechanism, but also let you do different things. Traits are basically like contracts and are instantiated and specialized at compile time with no runtime overhead. Trait objects are like interfaces or vtables. They give you runtime polymorphism at the cost of some performance.
- ori_b 7y ago> However, they operate at different times. This seems like it would be solvable by simply allowing interfaces to constrain generic parameters. Something like: func foo(type A Comparable, B OtherIface, C /* unconstrained */)(a A, b B) C { ... } You'd need to allow for type parameters on interfaces, of course: func lookup(type A Indexable(B), B Hashable)(set A, idx B) { ... } But there's no need, IMO, to separate interfaces and contracts.
- dilap 7y agoHow do u do something like a graph tho, that involves two types (nodes and edges)?
- ori_b 7y agoGraph(N,E) What's the problem you see?
- uryga 7y agoafaik there's no such thing as an interface for multiple types in Go; i.e. you can have (the equivalent of) `Foo implements Comparable` but not `Bar, Baz implement ConvertFromTo`. `Graph(N, E)` mentions two types, so you can't really express it as an interface, at least not in current Go.
- ori_b 7y agoBut this is a single interface, with two parameters. It's effectively just a function that returns an interface type. Graph(N, E) specialized with Graph(int, int) is roughly equivalent to: interface Graph_int_int
- uryga 7y agoah, that makes sense! but what about something like ConvertFromTo(F,T) ? i mean, we need a single method func convert(f F) T but where should it go (which type should implement it)?