Functions do not recon with `config.number` when converting a string into a numeric type
It's a bit of an edge case, but for example:
math.config({number: 'BigNumber'})
math.add('2', '3')
// results in a number with value 5, but should be a BigNumber with value 5
The reason is that functions like add do not have a signature like add(string, string). Therefore, automatic conversion kicks in. The automatic conversion finds a conversion from string -> number that fits the bill and uses that, without reconing with the configured config.number. I'm not sure how a proper solution to fix this would look like, since the type conversions are static 🤔.
@josdejong I looked into this and traced it back to the static string -> numeric conversions in src/core/function/typed.js.
One idea is to remove those conversions from the static conversion graph and instead handle numeric string inputs dynamically using the current config.
Before I go ahead, I have two quick questions:
- For mixed calls like
add('2', 3), shouldconfig.numberdecide the target type, or should the existing numeric argument decide it? - Would handling this in
typed.onMismatchbe acceptable, as long as it uses supportedtyped-functionAPIs and preserves existing custom typed-function behavior?
I just want to make sure the behavior and approach match what you expect before I open a PR.
Sadly those questions aren't quick. (Despite perhaps being AI generated? If so, please disclose. If not, my apologies to be suspicious, but it's forced on me by the onslaught of PRs etc that the few maintainers can't keep up with.)
There has been a lot of discussion around this sort of feature in a lot of different places; perhaps the most complete is #2734. You are absolutely right that a plan of attack is needed before implementing anything. When you feel you've read the relevant material in existing issues/discussions, and if you then feel like you have a concrete proposal and rationale, feel free to post here for feedback. Another alternative would be to open a master discussion with references to all the relevant places you can find, and direct readers there, and raise whatever points you have about a best course of action there. The latter altnerative would be better if you don't have a concrete proposal with rationale after absorbing the relevant material.
Thanks so much for your interest in mathjs, and we look forward to further discussion and hopefully eventually a resolution.
@josdejong I did some investigation without AI. However, I don't have a finished solution, just a hypothesis. I'll check the other discussions about this, and I think your suggestion to discuss it first and then decide on a possible solution is the best approach. Thanks!