ISTM that your requirements can't work. 2) and 3) imply that the expression
`x < y` with x,y of type T has to appear at least once in the function
body, which means it can never work for a type that doesn't have it.

I don't think that has anything to do with changing the type-checking rules
though. It's exactly what prevents contracts to be able to do it. In fact,
if it *would* be working, you could also make it work with contracts, by
just copying the function body into a contract.

On Wed, Sep 12, 2018 at 3:22 AM Patrick Smith <pat42sm...@gmail.com> wrote:

> This is a hypothetical question. Suppose generics were implemented as in
> the draft proposal, but without contracts. Instead, the rule is that if an
> instantiation of a generic function with a specific type argument doesn't
> compile, then it can't be used.
>
> Under those circumstances, is there any way to write a function
>
> func Min(type T) (a, b T) T {...}
>
>
> that returns the smaller of its two arguments, and meets _all_ of these
> criteria?
>
> 1) If a specific type X has a suitable Less() method, then Min(X) uses
> that.
> 2) Otherwise, if the < operator can be applied to X, then Min(X) uses that.
> 3) Otherwise, any use of Min(X) fails to compile. (A run-time panic does
> not satisfy this criterion.)
>
> By "X has a suitable Less() method", I mean that this function should
> compile successfully:
>
> func XMin(a, b X) bool {
>
>    return a.Less(b)
> }
>
>
> I'm fairly sure there's no way to write Min, but perhaps someone can prove
> me wrong?
>
> (With contracts I'm doubly sure it can't be done, as the contract for Min
> would have to permit both Less and <, so then Min(X) would only be allowed
> for types X having both Less and <.)
>

> --
> 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