I think that you really define many type systems for this ... This is a classic task for typed programming, which unfortunately does not include the Scala category.
You can look at Naturals at the Mark Harrah font level :
type _0 = Nat0 type _1 = Succ[_0] type _2 = Succ[_1] // ...
But if you go down this route, you will need to collect all your lists in such a way that the length type is obvious to the compiler. This means no recursion, no unlimited loop, etc. You will also have to come up with a way to encode "<" in the type system ... since you cannot perform recursion, I'm not sure how you will do it. So itβs probably not worth it.
Maybe another way to approach the problem is to find out where "0..5" comes from and restrain some other type based on this information?
As a last resort, you can define special cases for allowable sizes, divided so that you don't have 25 cases:
case class Small[+X](l: List[X]) def small(): Small[Nothing] = Small(List()) def small[A](a: A): Small[A] = Small(List(a)) def small[A](a1: A, a2: A): Small[A] = Small(List(a1,a2))
source share