--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
My intuition is that interfaces would be obsolete -- and that's a very good thing.
On Fri, Aug 31, 2018 at 6:57 PM Scott Cotton <w...@iri-labs.com> wrote:My intuition is that interfaces would be obsolete -- and that's a very good thing.They wouldn't. You can't have heterogeneous lists with contracts. For example, say you have a Reader contract:contract Reader(r R) {var (n interr errorp []byte)n, err = r.Read(p)}You can't use that to implement, say, io.MultiReader:func MultiReader(type R Reader) (readers ...R) R {return &multiReader{readers} // Type error: Must return R, not *multiReader}
func Foo() {r1 := bytes.NewReader([]byte("Hello "))r2 := strings.NewReader("world")r3 := MultiReader(r1, r2) // Type error: Uses different types in places of R}Saying that contracts subsume interface is a bit like saying that generic functions in Haskell subsume type classes. They are different things with different (but overlapping) uses.
On Saturday, 1 September 2018 18:26:29 UTC+2, Axel Wagner wrote:On Fri, Aug 31, 2018 at 6:57 PM Scott Cotton <w...@iri-labs.com> wrote:My intuition is that interfaces would be obsolete -- and that's a very good thing.They wouldn't. You can't have heterogeneous lists with contracts. For example, say you have a Reader contract:contract Reader(r R) {var (n interr errorp []byte)n, err = r.Read(p)}You can't use that to implement, say, io.MultiReader:func MultiReader(type R Reader) (readers ...R) R {return &multiReader{readers} // Type error: Must return R, not *multiReader}I don't think there would be a type error as follows:fun MultiReader(type R Reader) (readers ...R) Reader {return &multiReader{readers}}func Foo() {r1 := bytes.NewReader([]byte("Hello "))r2 := strings.NewReader("world")r3 := MultiReader(r1, r2) // Type error: Uses different types in places of R}
I don't understand what you are trying to say. You assert that there wouldn't be a type-error, but you don't actually justify that.
It seems pretty obvious to me, that if you instantiate my MultiReader example to, say, *strings.Reader, it would fail to compile. Because *multiReader is not a *strings.Reader (and you'd have to replace R with *strings.Reader everywhere in the signature, that is the exact reason we want generics to begin with).
The example you give for "unifying this" isn't actually syntactically correct, AFAICT. At least I can't find anything in the design that would allow using the identifier of the contract in its arguments - and it's unclear to me what that would mean.
To provide another way to clarify that interfaces and contracts are different:
* How would you build fmt.Println using contracts?
* One of the main motivators behind adding generics is the lack of type-safety for containers (say, container/list). While all methods mention the same type, interface{}, it is not checked for consistency between invocations. This is a bug for type-safe containers, but it's a feature for fmt.Println.