Skip to content

Update Coercion Rules #186

@JohnBrinkman

Description

@JohnBrinkman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions