Skip to content
New issue

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

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

Already on GitHub? # to your account

Add array method includes #1315

Open
Shadow-Devil opened this issue Jul 28, 2024 · 5 comments
Open

Add array method includes #1315

Shadow-Devil opened this issue Jul 28, 2024 · 5 comments
Assignees
Labels
Area/LangLib Relates to lang.* libraries Type/Improvement Enhancement to language design
Milestone

Comments

@Shadow-Devil
Copy link

Shadow-Devil commented Jul 28, 2024

Description

I'm quite surprised that there currently is no array.contains(item) method. Of course, I could use a query expression or array.indexOf(item) !is () or array.some(a => a == item) but I think it would be convenient to have this simple method.
Another idea is to use a similar syntax to Python (e.g. item in array) since in is already a keyword.

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Standard Library

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@MaryamZi
Copy link
Member

Moving this to the spec repo since this is a suggestion for the array langlib.

@MaryamZi MaryamZi transferred this issue from ballerina-platform/ballerina-lang Jul 30, 2024
@jclark
Copy link
Collaborator

jclark commented Sep 4, 2024

If we add it, it should be called includes for consistency with lang.string.

@jclark
Copy link
Collaborator

jclark commented Sep 4, 2024

@jclark
Copy link
Collaborator

jclark commented Sep 4, 2024

I think probably we should add for consistency with lang.string and ECMAScript/JavaScript. It should have an additional argument, defaulting to zero, saying where to start looking from.

@jclark jclark self-assigned this Sep 4, 2024
@jclark jclark added this to the 2024R2 milestone Sep 4, 2024
@jclark jclark added Type/Improvement Enhancement to language design Area/LangLib Relates to lang.* libraries labels Sep 4, 2024
@jclark jclark changed the title [New Feature]: Add array method contains Add array method includes Sep 4, 2024
@jclark
Copy link
Collaborator

jclark commented Sep 4, 2024

# Tests whether an array has a member equal to a value.
#
# + arr - the array in which to search
# + val - member to search for
# + startIndex - index to start the search from
# + return - `true` if there is a member of `arr` equal to `val` at an index >= `startIndex`,
#    or `false` otherwise
public isolated function indexOf(AnydataType[] arr, AnydataType val, int startIndex = 0) returns int? = external;

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Area/LangLib Relates to lang.* libraries Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants