#2795 List.getSafeRange?

andy Fri 22 May

I'm not sure the semantics -- but its always been cumbersome to manually check ranges. A List.getSafeRange would be nice. Maybe to keep it simple always return null if either start or end is out of bounds?

brian Fri 29 May

I like it, but think the semantics should be to clip which is how we did it in Axon. For example:

"abcd".getSafe(1..10)

clip option would returns "bcd"

null option would return null

The clip option seems more generally useful. Although maybe we call that one clip?

andy Sun 31 May

Eh I think if you passed in an explicit range it needs to "fail" with null. Otherwise you need to check the range anyways so doesn't save you any code.

But I'm not opposed to a "clipSafe" method either. Those are probably both valid use cases.

brian Wed 1 Jul

Any other comments?

Option 1: add getSafeRange which returns null when out of range

Option 2: add getSafeRange which returns clipped range

Option 3: add getClippedRange which return clipped range

Option 4: option 1 and 3 combined (two new methods)

Option 5: add getSafeRange with a flag to determine null/clip behavior

And whatever we add would also need to be added to Str too

andy Wed 1 Jul

Option #4

SlimerDude Mon 13 Jul

It opens up a lot of points already raised in List.getSafe but what of a default checked := true parameter?

It saves on polluting List with a lesser used method, it's consistent with a lot of other Fantom APIs, and is just as useful.

str := "abcd"
str.getRange(1..10, false)                // return null
str.getRange(1..10, false) ?: str[1..-1]  // return a default value

Returning null isn't such a hardship because you can always use elvis to set a default value.

Disregarding the method name, I guess I'm opting for Option 1!

Login or Signup to reply.