News for hackers. Stuff that matters.
Re:The Updated Draft Design for Go 2 Contracts by marcus_holmes on Saturday July 27, 2019 @02:20PM (#20543331)
as I understand it (and I may be misunderstanding it), contracts are exactly like interfaces, except the type of parameters/return values is left to the implementation.

So an interface of "adder" would look like:

    type adder interface {
        Add(int) int
    }
and it would only be able to support adding an int to something, and returning an int. Doing this:

    func AddStrings(a adder, b string) string {
        return a.Add(b)
    }
will break

A contract for "adder" would look like:

    contract adder(T) {
        T Add (T) T
    }
You can then use this in a function by:

    func addTheThings(type T adder)(t1, t2 T) T {
        return t1.Add(t2)
    }
which can then use any type that has an Add method

    //can't extend native types, so we have to create an alias and extend that
    type Addstring string

    func (a Addstring)Add(b Addstring) Addstring {
        return a+b
    }

    func (a Banana)Add(b Banana) Banana {
        return Banana{a.Count + b.Count}
    }
then your "addTheThings" function can operate on strings (via the Addstring type) and Bananas, and anything else that fulfills the contract. So the original example:

    func AddStrings(a adder, b string) string {
        if as,ok := a.(Addstring); ok {
            return a.Add(b)
        }
        //raise an error here
    }
On a separate note, why is "contract" becoming a keyword instead of "type foo contract {}"? Making the common business concept of "contract" a keyword is likely to break backward compatibility for some code bases
Re:The Updated Draft Design for Go 2 Contracts by dilap on Saturday July 27, 2019 @02:54PM (#20543507)
> On a separate note, why is "contract" becoming a keyword instead of "type foo contract {}"?

I was wondering about this too. It also just feels really weird since it's a different construction than any of the other types.

Maybe the thinking is because a contract is not a type? But so what...

On first blush, it also feels weird that type is coming _before_ the type arguments, rather than after -- since types for normal arguments come after the argument.

E.g.,

    Map(T, U type)(t []T, f func(T) U) []U
feels much more consistent than

    Map(type T, U)(t []T, f func(T) U) []U
no? (Perhaps it complicates parsing, though?)
Re:The Updated Draft Design for Go 2 Contracts by ithkuil on Saturday July 27, 2019 @08:32PM (#20545156)
"var n int" means "in this scope, declare variable n to be if type int".

"type T adder" means "in this scope, declare T to be a type adhering to the adder contract"

Re:The Updated Draft Design for Go 2 Contracts by dilap on Saturday July 27, 2019 @11:07PM (#20545632)
Good way to think of it.
Re:The Updated Draft Design for Go 2 Contracts by DangitBobby on Saturday July 27, 2019 @08:14PM (#20545081)
I think calling it a type would be confusing. It's not a type, it's a description of a behaviors of potential types.
Re:The Updated Draft Design for Go 2 Contracts by DangitBobby on Saturday July 27, 2019 @08:10PM (#20545065)
They address backwards compatibility in the document. It will only be a keyword in certain contexts so that any valid go1 code will still be valid go2 code.
Re:The Updated Draft Design for Go 2 Contracts by marcus_holmes on Sunday July 28, 2019 @11:31AM (#20548024)
ah, I missed that. Cool
Re:The Updated Draft Design for Go 2 Contracts by gigatexal on Sunday July 28, 2019 @12:50AM (#20545902)
Well done. I understood that. Thank you.