12 ms·
I will raise you with (type $T Comparable) func Max(a,b $T) $T { if a > b { return a } return b }
by tandr 6y ago
I will raise you with
(type $T Comparable) func Max(a,b $T) $T {
if a > b {
return a
}
return b
}
- tandr 6y agoInvocation maxInt := Max(1,2) maxUnk := (SomeType).Max(k,z) or maxUnk2 := Max@SomeType(k,z) or just maxUnk3 := Max(SomeType(k), SomeType(z))
- atombender 6y agoI like the idea of using special syntax to denote the type — $T signals that it is a higher-order placeholder. It means there's no ambiguity to the reader or the compiler. It also means that for a function that requires no type constraints, nothing more is needed: func WorksWithAllTypes(val $T) { ... } I don't like the type constraint syntax, though. I think it could be done like this: func Max(a, b $T Comparable) $T { ... } The idea here is that we only need to annotate the first instance of $T; all others will implicitly share the same type constraint.
- tandr 6y agoI posted semi-coherent thoughts on golang-nuts [1], but the message got completely ignored as for constrains, say if we use decorator, then since T has to adhere to one interface only, func Max from your example could become func Max(a, b $Comparable) $Comparable { // ... } [1] https://groups.google.com/forum/#!topic/golang-nuts/Rp3yUUy2nS8 https://groups.google.com/forum/#!topic/golang-nuts/Rp3yUUy2...