Skip to content
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

Workaround for Math variadic functions #82

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

aspeddro
Copy link
Contributor

@aspeddro aspeddro commented Mar 2, 2023

This ensures that the return for an empty array does not return -Infinity

TODO:

  • Update CHANGELOG

See #53

@zth zth added this to the 0.3.0 milestone Mar 9, 2023
Copy link
Contributor

@glennsl glennsl left a comment

Choose a reason for hiding this comment

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

Might it be better to just remove the @variadic? (edit: sorry, didn't mean to include this)

src/Core__Math.res Outdated Show resolved Hide resolved
Comment on lines 57 to 62
@variadic @val external _minMany: array<float> => float = "Math.min"
let minMany = arr =>
switch arr {
| [] => 0.0
| arr => _minMany(arr)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure the float versions ought to be overridden. Math.min([]) does indeed return 0, and Math.min() returns Infinity, but Math.minMany([1, 2, 3]) maps to Math.min(1, 2, 3), not Math.min([1, 2, 3]), and so it seems more consistent to have Math.minMany([]) map to Math.min(), not Math.min([]).

These are also edge cases that are very unlikely to occur in practice, since variadic calls require syntactic arrays there's little chance of it happening by accident. I'd suggest the aim of this is restricted to just making the types sound.

@aspeddro
Copy link
Contributor Author

aspeddro commented Jul 26, 2023

Might it be better to just remove the @variadic?

If you remove @variadic the compiler passes an array. Math.min([1, 2]) returns NaN

Playground

@glennsl
Copy link
Contributor

glennsl commented Jul 26, 2023

If you remove @variadic the compiler passes an array. Math.min([1, 2]) returns NaN

Ah, sorry, I didn't mean to include that. I checked after writing it, but apparently forgot to remove the comment. Please see the other comments instead :)

@aspeddro
Copy link
Contributor Author

I was wondering and not sure if we should return 0.0. Maybe let returns Infinity.

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