Efficiency of ranges - how are they stored?

I want to use a Range as return type, essentially to communicate what’s the highest and lowest index in another Collections custom data structure

I had a look at the doco to Range, but I am still not sure how lightweight ranges are: They can be overfull, so I guess they don’t store all elements individually, but do they really just store start, end and step?

Then, I think, it is viable to use them to return some min/max info.

Yes. (Well, those and the isInclusive flag.)

2 Likes

Indeed, it’s hard to tell from the Scaladoc.

It’s hard to tell why NumericRange is something to avoid, as opposed to regular Range.

The other consideration is that you’re loading a bunch of collections classes, which is less of a concern if you’re already doing that for a custom collection.

But in general, a simple custom case class for bounds seems less fraught to me.

Then instead of falling into traps of the standard API, you could define

def slice(bounds: Bounds): MyCC

Someone was just saying how deceptive these signatures can be. Is it (start, end) or (index, count)? Inclusive interval? and so on. What about negative or swapped values?

But that depends on whether you have a collection that wants to leverage the standard API, of course.

1 Like

Good advice. I wasn’t aware of this.

Interesting aspect. Thanks.

True. However I might still go with Range, just to get as a beginner more familiar with the Standard Library classes

Thanks for all the details!