feat: add ndarray/base/scalar-dtype - #15157
Conversation
Resolves the TODO in `ndarray/base/atleastnd` by extracting the
scalar-to-dtype resolution that is currently written out twice:
ndarray/base/atleastnd/lib/main.js:111
ndarray/from-scalar/lib/main.js:128
Both spell the same four branches -- number to the default real-valued
floating-point dtype, boolean to the default boolean dtype, complex to a
corresponding complex dtype falling back to the default complex-valued
floating-point dtype, and everything else to `generic`.
Behaviour matches the existing implementations exactly, including
resolving `Number` and `Boolean` objects to `generic`, since both call
sites test primitives via `isPrimitive`.
Adds the package to the `ndarray/base` namespace and its TypeScript
declarations, in package-path order alongside `rotr90` and
`serialize-meta-data`.
Ref: stdlib-js#11002
|
👋 Hi there! 👋 And thank you for opening your first pull request! We will review it shortly. 🏃 💨 Getting Started
Next Steps
Running Tests LocallyYou can use # Run tests for all packages in the math namespace:
make test TESTS_FILTER=".*/@stdlib/math/.*"
# Run benchmarks for a specific package:
make benchmark BENCHMARKS_FILTER=".*/@stdlib/math/base/special/sin/.*"If you haven't heard back from us within two weeks, please ping us by tagging the "reviewers" team in a comment on this PR. If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members. We appreciate your contribution! Documentation Links |
|
Hello! Thank you for your contribution to stdlib. We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do:
This acknowledgment confirms that you've read the guidelines, which include:
We can't review or accept contributions without this acknowledgment. Thank you for your understanding and cooperation. We look forward to reviewing your contribution! |
|
Similar comments as discussed in #11003 apply to this PR. |
|
Thanks for the quick pointer — and apologies, this is a duplicate. I missed #11003 before opening this, which is entirely my fault: I checked that #11002 was unassigned and had no comments, but did not finish checking for an existing open PR against it. @Aniket-SS opened both on the same day and has been on this since March; I shouldn't have landed on top of that. Happy to close this in favour of #11003 — just say the word and I will. I'd rather not have two PRs sitting on one issue. On the substance, you're right that the comments carry over. Mine returns plain strings: return DEFAULT_REAL; // 'float64'
...
return 'generic';so it makes exactly the assumption you flagged on #11003. The one place the two differ is the other point: this PR only adds the package and doesn't touch Since I'm here and it seems to be the open question that stalled #11003 — @Aniket-SS asked in March whether out[ dt[ i ] ] = new DataType( dt[ i ] );so resolving through that table would return an object while keeping the four branches as they are. If that's the intended shape, I'm glad to hand it over — or to rework mine and close #11003 instead, whichever is less disruptive. @Aniket-SS, your call as much as anyone's; you filed the RFC. Either way, sorry for the noise. |
Resolves #11002.
Description
This pull request:
ndarray/base/scalar-dtype, which resolves a default ndarray data type from a scalar value.The logic currently exists in two places:
ndarray/base/atleastnd/lib/main.js:111— which carries the TODO this implementsndarray/from-scalar/lib/main.js:128Both spell the same four branches: a number resolves to the default real-valued floating-point dtype, a boolean to the default boolean dtype, a complex number to a corresponding complex dtype (falling back to the default complex-valued floating-point dtype when unrecognized), and everything else to
'generic'.Related Issues
This pull request has the following related issues:
ndarray/base/scalar-dtype#11002Questions
Two:
ndarray/base/atleastndorndarray/from-scalarto use the new package, so it can be reviewed on its own. Would you prefer that folded in here, or as a follow-up?NumberandBooleanobjects resolve to'generic', because both existing call sites test withisPrimitive. I preserved that rather than widening it, since changing the behaviour would change both consumers the moment they adopt this — but say if you'd rather it accepted boxed primitives.Other
The package follows
ndarray/base/min-signed-integer-dtypeas its structural reference, and is registered in thendarray/basenamespace (lib/index.js) and TypeScript declarations (docs/types/index.d.ts), placed betweenrotr90andserialize-meta-datato match that file's package-path ordering.A full
npm installwas not available in my environment, so I ran the test, example, and benchmark files directly rather than throughmake. Local results:Test coverage includes numbers (
NaNand both infinities included), booleans,Complex128andComplex64, a complex-like object with an unrecognized dtype, and the non-scalar cases — strings,null,undefined, arrays, plain objects, functions, and the boxedNumber/Booleancases above. Since lint was not run locally, please flag anything CI turns up and I will fix it.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written with Claude Code. It was used to locate the duplicated logic from the TODO in #11002, to read
min-signed-integer-dtypeas a structural reference, and to draft the implementation, tests, benchmark, docs, and TypeScript declarations. I verified the behaviour case by case against the two existing implementations and ran the tests, examples, and benchmark locally before opening this.@stdlib-js/reviewers