4 ms·
T max<T>(Comparable a, Comparable b) { if (a.compareTo(b) < 0) { return a; } else { return b; } } Or something.. Explicitly specifying that a and b needs to
by d0m 17y ago
T max<T>(Comparable a, Comparable b) {
if (a.compareTo(b) < 0) { return a; }
else { return b; }
}
Or something..
Explicitly specifying that a and b needs to be comparable instead of shooting an endless template error message.
And: "My approach works, theirs does not work." explains perfecly why C++ is what it is.
- jpr 17y agoEquality and comparisons in presence of subclassing etc. are not the business of a sane man. I think it is impossible to write an .equals() in java that satisfies all the relevant laws people usually assume from it.
- joshhart 17y agoIn Eclipse: Alt-Shift-T for refactor. Select "Generate Hash-code and Equals".
- mhansen 17y agoequals(Object o) has a very tricky contract to implement, when you add subclassing to the mix. Auto-generated code won't make those problems go away. See http://www.artima.com/lejava/articles/equality.html http://www.artima.com/lejava/articles/equality.html
- joshhart 17y agoI was mostly joking when I wrote the parent comment. I've read Joshua Block's Effective Java which spends a long time discussing the challenges of properly implementing equals. Generally speaking, Eclipse gets it correct. Implementing equals properly boils down to following a set of rules - something that computers are exceptionally good at.
- jrockway 17y agoI upmodded this for comedy value. This is really how Java programmers think. So what if the code is wrong in corner cases, it was auto-cut-n-pasted!
- sketerpot 17y agoIt's possible to be locally sane. For example, suppose you define an abstract Number class, and define subclasses for ComplexFloat and IntFraction numbers. You could define .equals() methods on each of those that only compare to the same type, and work correctly. You don't always have to deal with weird issues.
- huhtenberg 17y ago"Your approach" is what Stepanov was referring to as "concepts" about .. erm .. 10 (?) years ago. It is effectively a template argument constraint system. Concepts were one of the most expected features of C++0x, the one that got just recently dropped from the consideration due to being "not ready".
- sketerpot 17y agoIs that similar to this Haskell code? max :: Ord a => a -> a -> a max a b = if a > b then a else b The concepts that Stepanov was talking about, where algorithms are defined for classes of data structures supporting certain operations (iteration, comparison, etc.), sounds very familiar to anyone who's familiar with Haskell's type classes.
- tumult 17y agoYep
- jemfinch 17y agoThat Haskell code, as I understand it, will dynamically dispatch the call to max through the Ord typeclass dictionary passed at runtime. The C++ code will statically choose the appropriate function.
- klipt 17y agoI'm pretty sure that Haskell typeclasses are also resolved at compile time. That's why you can't have a list containing different ordered types, for example (unless they can all be coerced to the same type).
- jemfinch 17y agoNo, you can't have a list containing different ordered types because the signature for comparison only permits two values of the same exact type, not any arbitrary member of class `Ord`. As I said before, Haskell passes [runtime dictionaries](http://www.haskell.org/haskellwiki/OOP_vs_type_classes http://www.haskell.org/haskellwiki/OOP_vs_type_classes). The compiler can perhaps infer many cases statically, but the core implementation is to pass around runtime dictionaries for typeclasses.
- brendano 17y agoThe article is old, from when Java was new. His criticism was correct back then. Java didn't get generics until 8 years after it was introduced. (Java 1.5: http://en.wikipedia.org/wiki/Java_version_history http://en.wikipedia.org/wiki/Java_version_history )