Open fonsp opened 2 years ago
Alternatively, should this particular method of Base.length
use checked integer arithmetic?
Good idea! I implemented this in #43429. The FR remains (it is still not possible to get the length), but my use case ("does x have fewer than y elements?") is covered by doing checked arithmetic, because I can catch for the OverflowError
.
since the original example now gives an OverflowError
and can be addressed by using BigInt
ranges, I think this issue can be closed
For some built-in Julia types, calling the
length
function causes integer overflow:This caused a problem for me, where I was checking "is this object small enough to send over the network?", and the second object gives a false postive. It seems like Base Julia has no built-in way to avoid this.
My feature request would be a to standardise a method
length(T::Type, x)
, where the first argument provides the output type. In my case, I would use it likelength(BigInt, x)
, and the default islength(Int, x)
.Usage
With a fallback
length(T::Type, x) = convert(T, length(x))
, this could gradually be added to the ecosystem:For example, the new method for the
ProductIterator
would look like: