Re: Collection.size behavior
On Apr 30, 8:45 pm, Benjamin <musiccomposit...@gmail.com> wrote:
Does anyone know the reason that Collection.size returns
Integer.MAX_VALUE when the the collection size is greater than that?
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?
In Java, if a method's signature declares that it returns a type, the
implementation cannot return an incompatible type. For reasons known
only to the Java 1.2 team, the Collections API uses 'int' as the
return type from Collection.size(), so the largest value that can
possibly be returned is Integer.MAX_VALUE.
Not so in Python: methods have no signatures as such, and (as with
smalltalk, ruby, lisp, and many other languages) arithmetic on bignums
is identical to and compatible with arithmetic on machine integers.
Report the real size, if the size is available at all; use a bignum if
you have to. Your users won't notice the difference (except perhaps
in speed, but generally size isn't called in a tight loop) and your
API will be consistent between small, in-memory collections and
massive or procedurally-generated collections.