Skip to content

Update Array#2987

Open
sampersand wants to merge 15 commits into
ruby:masterfrom
sampersand:swesterman/2026-05-22/update-Array
Open

Update Array#2987
sampersand wants to merge 15 commits into
ruby:masterfrom
sampersand:swesterman/2026-05-22/update-Array

Conversation

@sampersand
Copy link
Copy Markdown
Contributor

@sampersand sampersand commented Jun 3, 2026

This PR completely updates Array's signatures, and rewrites the test harness to work with it.

Note that there's a handful of TODOs strewn about in:

  1. Places that will be updated once Add RBS::Ops  #2934 lands
  2. Markers for functions that should be updated once "where" clauses are finalized
  3. Miscellaneous indicators where the signature isn't quite what source code would support (eg Array.[])

@sampersand sampersand changed the title swesterman/2026 05 22/update Array Update Array Jun 3, 2026
@sampersand sampersand force-pushed the swesterman/2026-05-22/update-Array branch 2 times, most recently from 786a245 to 31a4a54 Compare June 3, 2026 21:28
Comment thread core/array.rbs
Comment on lines -1158 to +1166
alias any? all?
def any?: () -> bool
| (_Pattern[E] pattern) -> bool
| () { (E element) -> boolish } -> bool
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was probably originally aliased because they share the same signature? but they shouldn't have been

Comment thread core/array.rbs
#
alias any? all?
def any?: () -> bool
| (_Pattern[E] pattern) -> bool
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Use RBS::Ops:_Matches

Comment thread core/array.rbs
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
#
def include?: (top object) -> bool
def include?: (top object) -> bool # TODO: E: _Equals
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be top or untyped? And should other places be using top?

Comment thread core/array.rbs
| (int n) -> ::Array[E]
| (int n) { (E a, E b) -> ::Integer? } -> ::Array[E]
def max: %a{implicitly-returns-nil} () ?{ (E a, E b) -> Comparable::_CompareToZero? } -> E # TODO: where `E: Comparable::_WithSpaceshipOperator`
| (int count) ?{ (E a, E b) -> Comparable::_CompareToZero? } -> Array[E] # TODO: i dont think these shoudl have `?` in _CompareToZero. But it would make signatures nicer?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Should we have ? here in the signature? Actually having it will always cause an exception, but most <=> have nil in their return type.

Comment thread core/array.rbs
@@ -3292,7 +3302,7 @@ class Array[unchecked out E] < Object
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
#
def shift: %a{implicitly-returns-nil} () -> E
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to have this be an implicitly-returns-nil

Comment thread core/array.rbs
#
def sum: (?untyped init) -> untyped
| (?untyped init) { (E e) -> untyped } -> untyped
def sum: (?untyped init) ?{ (E e) -> untyped } -> untyped # This would be usable if we had `where E < Complex`, etc
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there was a better way to solve sum

Comment thread core/array.rbs
def rand: (Integer max) -> Integer
end

interface Array::_Pattern[T]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: deprecate this and just use RBS::Ops when needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, do this

@sampersand sampersand force-pushed the swesterman/2026-05-22/update-Array branch 2 times, most recently from 1eef064 to 0fa718e Compare June 3, 2026 21:38
@sampersand sampersand marked this pull request as ready for review June 3, 2026 21:49
@soutaro soutaro added this to the RBS 4.1 milestone Jun 4, 2026
Comment thread core/array.rbs
def bsearch: () -> ::Enumerator[E, E?]
| () { (E) -> (true | false) } -> E?
| () { (E) -> ::Integer } -> E?
def bsearch: () { (E element) -> (Numeric | bool?) } -> E?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this works for actual type checkers, but these definitions are not equivalent.
Returning both true | false and Integer from a block is not allowed to bsearch, I think.

Copy link
Copy Markdown
Contributor

@ParadoxV5 ParadoxV5 Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[].bsearch { it.positive? and it } # false | Integer

Comment thread core/array.rbs
#
def sort!: () -> self
| () { (E a, E b) -> ::Integer } -> self
def sort!: () ?{ (E a, E b) -> Comparable::_CompareToZero } -> Array[E]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it return self instead of Array[E]?

@sampersand sampersand force-pushed the swesterman/2026-05-22/update-Array branch from cc52661 to 4691c6d Compare June 4, 2026 06:20
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.

3 participants