-
Notifications
You must be signed in to change notification settings - Fork 216
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
Type signature updates for Enumerable, Module, Range, Array #1921
Conversation
d5f9d23
to
661d04a
Compare
`enum_for` and `to_enum` default to creating an Enumerable based on the `each` method. Due to this, we can improve the type signature (make it more specific) for classes that include the Enumerable mixin as their `each` method iterates over elements of type `Elem`.
…exec` This update ensures the type signature matches the documentation, which specifies that any arguments passed to the method call are forwarded on to the block, as well as a self type of `self`.
… optional parameters)
…hods that may return `nil` The methods chosen are only those that are similar to the access method (`[]`); methods that return nil if the array is empty or the index is out of bounds.
0f3578d
to
134d2df
Compare
The purpose of |
I agree, better to keep it as such.
I believe that method contracts do have a purpose in signatures and would be of help, but currently they do not exist and it looks like it's still under discussion. In the meantime, I believe this has value as it is still a standard practice for a subclass in OO languages to provide narrower types. The commit regarding As a side note, if there are only concerns with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Let's try with this version, which makes a deadline to support it in Steep. 😅
Thanks! Yeah let's see how it goes :), no concerns from me if it needs to be changed/reverted at some point. |
These are some type signature updates, mostly following on from the discussion here: #1226, but also some additional ones on top of it.
I've made the following changes:
Enumerable
- added a more specific type override forenum_for
/to_enum
for the default (:each
/no-arg) case; this override means that we know theEnumerator
returned will be iterating over elements of typeElem
. I think that this is a sensible override to provide because of the type from_Each
. I wasn't sure if the overrides should go here or in the_Each
interface, but it felt strange putting more things into_Each
when it's currently clean and simple.Module
- improved the type signature forclass_exec
to be similar toinstance_exec
. Specifically it now shows that arguments passed are forwarded to the block, and that the block has aself
type based on the receiver.Range
- added a type signature for%
as it was missing. This one is a copy/paste fromstep
, minus the optional parameters.Array
- added some more%a{implicitly-returns-nil}
annotations. I've only added them in locations that looked similar to the original for[]
, where they may returnnil
if an index is out of bounds or the array is empty, as this is typically knowledge that is checked beforehand. Happy to reduce the number added though if it is too many. I haven't added any toEnumerable
as there is no size/length/empty methods, soHash#first
will not have this annotation.Let me know if there's anything I missed or items you'd like me to change.