Skip to content

Add more types for Random#470

Merged
soutaro merged 2 commits into
ruby:masterfrom
zofrex:random
Nov 24, 2022
Merged

Add more types for Random#470
soutaro merged 2 commits into
ruby:masterfrom
zofrex:random

Conversation

@zofrex
Copy link
Copy Markdown
Contributor

@zofrex zofrex commented Nov 9, 2020

This is my first time writing RBS so I am not sure it is right, especially the tests.

I am not sure how to fully describe rand because it can return whatever type you give it, for example:

> Random.rand(Tally.new("|")..Tally.new("|||||"))
=> #<Tally:0x00007fedf725f9f8 @string="|">

In other words, rand: (Range[SomeType]) -> SomeType but I don't know how to write that in RBS.

(Also, I think SomeType might have to be a descendent of Numeric, but the documentation is not clear)

Comment thread core/random.rbs
#
def rand: () -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Integer | ::Range[Integer] | Numeric max) -> Integer
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.

Seems like having Numeric here will return Integer for any numbers including Float.
Moving this line after next line would give better type.

Comment thread core/random.rbs
| (Integer | ::Range[Integer] max) -> Integer
| (Integer | ::Range[Integer] | Numeric max) -> Integer
| (Float | ::Range[Float] max) -> Float
| (::Range[Numeric]) -> Numeric
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.

Yeah, the type we want would be [T <: Numeric] (Range[T]) -> T, but RBS currently doesn't allow writing it.

I think there are two options. [T] (Range[T]) -> T or (Range[Numeric]) -> Numeric. The first option would make sense in my opinion, because testing the code will uncover the problem anyway.

Copy link
Copy Markdown
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

@zofrex Thanks for opening this pull request! Commented inline.

@snaka snaka mentioned this pull request Jan 25, 2022
soutaro added a commit that referenced this pull request Nov 24, 2022
Add more types for Random (fix conflict #470)
@soutaro soutaro merged commit a8238c0 into ruby:master Nov 24, 2022
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.

2 participants