Hello, @anna_shaw and welcome!
Your local level one support grug here. 
Let me check I understand you correctly … you want a sorted set of things that are ranges, using a custom range type you’ve written yourself?
(BTW: You could just use Range
from the standard library, but I understand if you are doing this from scratch to limber up in Scala in general.)
Just to be clear, do you really want your T
to extend a Range[?]
? This seems odd in two ways:
- One being that I presume you really want a
T[Underlying] <: Range[Underlying]
for some given generic Underlying
; i.e. you don’t want to park ranges of integers side by side with ranges of doubles and ranges of strings in the same set. If you want to keep the ranges over the same type per set instance, you could introduce Underlying
as an extra generic type parameter, working in terms of T[Underlying]
.
- The other being that extending case classes isn’t really the done thing in Scala, for the most part they are generally treated as final; so I’d just work with the
Range
type and stop there.
(I expect the likes of @BalmungSan or @DmytroMitin to be along soon to remind me not to mix wildcard syntax, type constructor syntax and old Scala 2 syntax up, which I do on a regular basis.)
So if you just want a sorted set of ranges of some type, let’s call it Underlying
, then how about a simple type lambda in Scala 3?
type SortedRangeSet[Underlying] = [Underlying] =>> SortedSet[Range[Underlying]]
Or more concisely:
type SortedRangeSet = SortedSet[Range[_]]
(Had to edit the above as once again, wildcards versus type constructors, etc…)
That would police client code’s use like this:
val yep: SortedRangeSet[Int] = SortedSet(Range(0, 1)) // That's fine.
val nope: SortedRangeSet[Int] = SortedSet(1) // Does not compile.
val chancer: SortedRangeSet[String] = SortedSet(Range(0, 1)) // Thought we might get away with it, but does not compile either.
I’m not sure whether my response is pitched at the right level, given the sophistication of your Scastie example. Are you trying to solve some much more complex problem? Was an LLM involved?
Anyway, hope that helps. If you really want to roll your own collection, you could look at: Implementing Custom Collections (Scala 2.13) | Scala Documentation for inspiration.