-
Notifications
You must be signed in to change notification settings - Fork 8
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
Update Coercion Rules #186
Comments
Not sure I agree. In all these cases type coercion is, pretty much, the same and returns the "default" value of the type. Whether it make sense or not is more context dependent. For function arguments like I would say it would make sense if either all of them threw an error (akin to a strict language with NullPointerException) or none of them did (akin to a more lenient language with default values). The main concern with the first approach, that you would need to wrap input/form data with Another alternative might be to leave Also it's not only It also means, that Not sure, what the best solution for this is, besides removing those conversions as well... |
Another option for the null issues is that we adopt Proposal 2, but we change function signatures to explicitly allow null. On the other examples, as a result of "Proposal 1" they would not throw errors. |
Yeah, I was also thinking about that. In that case you could "force" it to coerce to a scalar. Though, depending on the context, if Also, after some more thinking, my alternative with
Oh... I was thinking this is similar to the Don't remember, whether there is explicit information on coercing to |
Agreed. e.g. The spec indicates that we will coerce a number to a string, but doesn't explicitly say we'd convert a number array to a string array. There is yet another possibility for handling nulls inside arrays. For aggregating numeric functions (max, min, sum, stddev etc) we should consider changing the behavior to ignore null entries in the array. |
With the proposed changes for issue #185 (Allow functions to process arrays) we will have many function calls that currently work, but will fail once many functions accept more parameter types. Those failures happen because of the coercion rules.
The changes proposed here modify our coercion rules so that many of those function calls will continue to work.
These modified rules are breaking changes, that would be combined with the changes of #185 to define json-formula 2.0.0
Proposal 1
Parameters to functions shall be coerced when there is a single viable coercion available. For example, if a null value is provided to a function that accepts a number or string, then coercion shall not happen, since a null value can be coerced to both types. Conversely if a string is provided to a function that accepts a number or array of numbers, then the string shall be coerced to a number, since there is no supported coercion to convert it to an array of numbers.
Proposal 2
Today, a null value can be coerced to all other data types: number (
0
), string (""
), boolean (false
), array ([]
) and object ({}
).The problem is that in the context of function calls we never coerce nulls to a function parameter with multiple types, since nulls can be converted to anything.
For example, calling
upper(null)
will fail because upper() accepts either a string or array of strings, and null can be coerced to either.We propose that we no longer coerce null values to empty arrays and empty objects.
It makes sense to convert nulls to a number, string or boolean. It makes less sense to convert to an array or object.
With this change many functions will not throw an error when passed a null. e.g. abs(
null
) will return zero rather than throw a TypeError.The text was updated successfully, but these errors were encountered: