On Thu, Sep 6, 2018 at 11:36 PM jimmy frasche <soapboxcic...@gmail.com>
wrote:

> To unify those cases you need something like haskell-style typeclasses
> where you can say this type satisfies this contract using these
> methods/operators.
>

FWIW, the Go way of doing that is embedding it in a new struct and
overwriting the methods - which is pretty much the only way to do it while
maintaining the property that only the owner of a type can define methods
on it.

So, you can already do this. But it requires you to write it. Which is
boilerplate.
(which I'm personally fine with, FTR :) )


>
>
> Even ignoring that, the contracts proposal doesn't let you write a
> generic variadic min/max since there's no way to get the
> smallest/largest value of a type so you can't write
>
> func min(type T ordered)(ts ...T) T {
>   min := LargestPossible(T) // not possible
>   for _, t := range {
>     if t < min {
>       min = t
>     }
>   }
>   return min
> }
>
> You could get around that with the signature (firstOne T, rest ...T)
> but there's a larger point about the necessity of knowing numeric
> limits and properties for writing that kind of generic code in there.
>
>
> One way around the asymmetry between operators and methods that
> doesn't introduce any issues like operator methods or any of that is
> to have a package that defines types like
>
> package basic // not the best name
>
> type Int int
>
> type Slice(type T) []T
>
> // and so on for all the primitive and basic composite types
>
> and give all of those methods like Less and At. You'd have to do
> int(min(basic.Int(x), basic.Int(y))) but at least you wouldn't have to
> write the trivial wrapper methods. Not especially pretty.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to