Re: Collection.size behavior
Benjamin wrote:
Does anyone know the reason that Collection.size returns
Integer.MAX_VALUE when the the collection size is greater than that?
1. The brunt of the API prefers 32-bit integers unless that is
incapable, and Integer.MAX_VALUE is the largest 32-bit integer Java can
represent. The other choices would have been to return a negative
number, e.g., -1, which is not intuitive, or to return an exception,
which violates the maxim of exceptions representing `exceptional'
circumstances.
The reason I'm asking is because we, the Python (programming language)
developers, are considering imitating this with our sequences. It
seems to be that this is akin to silently lying and could be quite
confusing. Am I missing some practical benefit from this?
My opinion, along with most of the others in this thread, is to not
emulate this quirk. It is Java's nature which requires the return value,
and python does not have the same structures hindering it.
That said, I would not call it "silently lying," but more an
understatement. AFAICT, the collections that Java provides (excluding
wrappers) cannot have a size greater than Integer.MAX_VALUE anyways, so
the idea was probably a future-proofing decision. In addition, I feel
that most collections code which needs the precise size would fail with
collections for which the return value is wrong anyways, so the return
value issue is more or less moot.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth