Skip to content

Fix UNKNOWN default parameters of rand() and mt_rand() #6184

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

Closed
wants to merge 1 commit into from

Conversation

dtakken
Copy link

@dtakken dtakken commented Sep 22, 2020

Squash some more UNKNOWN defaults.

Comment on lines 310 to 313
if (argc == 0) {
// genrand_int31 in mt19937ar.c performs a right shift
RETURN_LONG(php_mt_rand() >> 1);
}
Copy link
Member

Choose a reason for hiding this comment

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

These blocks should come after ZPP and you need to make the function work with one argument now for this to work.

@nikic
Copy link
Member

nikic commented Sep 22, 2020

Please see #6026 (comment).

@dtakken
Copy link
Author

dtakken commented Sep 22, 2020

Ah yes, of course. The mt_rand() function could previously not be called with a single argument. Now it can, and we want its behavior to be intuitive. The following call is a little problematic in that regard:

mt_rand(10)

This will not do what you expect from the looks of it. However, this can be solved by using named arguments:

mt_rand(max: 10)

Another option that crossed my mind is to have the two parameters swap positions and accept it when min > max (just like rand() does) for backward compatibility. Then the following things will all work as you would expect:

mt_rand()
mt_rand(10)
mt_rand(5, 10)
mt_rand(min: 5)

Getting rid of the UNKNOWN parameters is nice, but there are no perfect solutions to do it...

@nikic
Copy link
Member

nikic commented Sep 23, 2020

As I said before, I don't think we should change anything here. The use of UNKNOWN is okay for overloaded signatures like these.

@dtakken
Copy link
Author

dtakken commented Sep 23, 2020

Ok, closing this PR.

@dtakken dtakken closed this Sep 23, 2020
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