rhombus-prototype icon indicating copy to clipboard operation
rhombus-prototype copied to clipboard

Generalize Ranges to support characters and other comparable values

Open avramlevitter opened this issue 9 months ago • 2 comments

Many other languages support the concept of a range that can be any sequence that meets simple criteria, beyond just integer sequences. I was looking to use a shorthand to create a list of characters from 'a' to 'z' but ranges only supports Integers, which means I needed to do

Range.from_to_inclusive(Char"a".to_int(), Char"z".to_int()).to_list().map(Char.from_int)

This could also apply to other kinds of sequences, such as dates.

avramlevitter avatar Mar 27 '25 14:03 avramlevitter

Generalizing ranges is something we're interested in, but not focused on currently. There's some design questions to sort out, like whether ranges will be restricted to Comparable values or whether they'll accept any value and include a corresponding Comparator object.

jackfirth avatar Mar 28 '25 04:03 jackfirth

start ..= end ranges also work as sequences, so I’d write

for List (i in Char"a".to_int() ..= Char"z".to_int()):
  Char.from_int(i)

This is still sub-optimal, admittedly. Ideally, (Char"a" ..= Char"z").to_list() should work just fine.

usaoc avatar Mar 28 '25 07:03 usaoc