stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Argument labels for bit_array.slice and string.slice are inconsistent

Open sarna opened this issue 3 months ago • 7 comments

// bit_array
pub fn slice(
  from string: BitArray,
  at position: Int,
  take length: Int,
) -> Result(BitArray, Nil)

// string
pub fn slice(
  from string: String,
  at_index idx: Int,
  length len: Int,
) -> String

It would be nice if they had the same labels. I tried looking for more functions like that, but I didn't find anything else.

sarna avatar Apr 05 '24 16:04 sarna

This could be upgraded with optional args, but without them, this is a breaking change. However stdlib is not yet 1.0 so this might still be doable, I hope.

inoas avatar Apr 05 '24 16:04 inoas

What should we make them?

lpil avatar Apr 12 '24 11:04 lpil

Personally I like the bit_array ones much better.

Now I noticed the return type is different too, that's unfortunate :(

sarna avatar Apr 12 '24 12:04 sarna

Also, string.slice lets you use negative indices to slice starting at some offset from the end of the string, while bit_array.slice doesn't. Maybe both could accept that?

PgBiel avatar Apr 14 '24 05:04 PgBiel

It's not possible as you can't negatively traverse a string, graphemes are not of constant size.

lpil avatar Apr 19 '24 13:04 lpil

Thanks for the input @lpil . I think my reply might have been a bit confusing (sorry!), so just to clarify: I meant being able to pass a negative number to the "position" argument in bit_array.slice for convenience, so slicing at a position of -4 with a length of 4 would give you the 4 last bytes, for example. Mostly because string.slice seems to allow that. Does this sound like a good idea, or should users just use byte_size(bit array) anyway?

PgBiel avatar Apr 19 '24 13:04 PgBiel

Oh! If strings support it then we should support it for bit arrays too

lpil avatar Apr 20 '24 11:04 lpil