Skip to content

[stdlib] IntRange substring functions as get operators#5746

Open
JordanLongstaff wants to merge 1 commit intoJetBrains:masterfrom
JordanLongstaff:substring-operator
Open

[stdlib] IntRange substring functions as get operators#5746
JordanLongstaff wants to merge 1 commit intoJetBrains:masterfrom
JordanLongstaff:substring-operator

Conversation

@JordanLongstaff
Copy link

Not a huge motivation for this, call it syntactic sugar. I just don't know why this doesn't already exist.

@kotlin-safe-merge
Copy link

Code Owners

Rule Owners Approval
/​libraries/​stdlib/​ @DmitryNekrasov, @fzhinkin, @ilya-g, @qwwdfsad

@lppedd
Copy link
Contributor

lppedd commented Mar 16, 2026

This gets very awkward. Pretty much every developer in every language is used to either indexed access (str[index]) to get a single character, or to str.substring(start, end) for a piece of a string. Introducing a "fake" indexed access with a range is both confusing and error prone, but that's just my opinion.

@JakeWharton
Copy link
Contributor

JakeWharton commented Mar 16, 2026

Pretty much every developer in every language is used to either indexed access (str[index]) to get a single character, or to str.substring(start, end) for a piece of a string.

Python:

'hello, world'[1:6]

Rust:

let a = "hello, world";
let b = &a[1..6];

Ruby:

"hello, world"[1..6]

C#:

"hello, world"[1..6]

Julia:

"hello, world"[1:6]

and on…

@lppedd
Copy link
Contributor

lppedd commented Mar 16, 2026

The literal form is probably fine and understandable. However once you extract the range into a separate variable it loses most of its sense and readability.

@JakeWharton
Copy link
Contributor

Do you also switch to invoking the named function when your indexing value changes from a literal to a variable?

It's just a variable naming problem.

@lppedd
Copy link
Contributor

lppedd commented Mar 16, 2026

Index access has been there forever, so that point doesn't really make a lot of sense imo.
Kotlin's goal is to prioritize readability. But I'm obviously not here to block useful features if the community feels the need for them. I personally don't like expanding the (already quite large) standard library with non-critical missing features.

@JakeWharton
Copy link
Contributor

I'm not sure what the age of the capability has to do with it. You claimed that when one transition's from a literal to a variable that the special syntax no longer makes sense and the named function should be used.

However once you extract the range into a separate variable it loses most of its sense and readability.

Surely that holds true for plain indexing then, too?

@lppedd
Copy link
Contributor

lppedd commented Mar 16, 2026

I'm not sure what the age of the capability has to do with it

When you see str[whatever] it is obvious we're talking about getting a single character at whatever index.
Introducing these two functions means whatever might be a range, and you're actually getting a substring. Code you read and understood yesterday might not have the same meaning anymore.

@kshulzh
Copy link

kshulzh commented Mar 17, 2026

Hi all, what about this option?
public inline operator fun String.get(start: Int, end: Int): String = substring(start, end)
usage
val hello ="Hello World"[0,5]
`

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

Successfully merging this pull request may close these issues.

4 participants