Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sarna opened this issue Apr 5, 2024 · 7 comments
Open

Comments

@sarna
Copy link
Contributor

sarna commented Apr 5, 2024

// 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.

@inoas
Copy link
Contributor

inoas commented Apr 5, 2024

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.

@lpil
Copy link
Member

lpil commented Apr 12, 2024

What should we make them?

@sarna
Copy link
Contributor Author

sarna commented Apr 12, 2024

Personally I like the bit_array ones much better.

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

@PgBiel
Copy link
Contributor

PgBiel commented Apr 14, 2024

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?

@lpil
Copy link
Member

lpil commented Apr 19, 2024

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

@PgBiel
Copy link
Contributor

PgBiel commented Apr 19, 2024

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?

@lpil
Copy link
Member

lpil commented Apr 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants