https://github.com/JuliaLang/julia
Revision 8d577ab3f24212a9762ad6673e8a8fb0ecd5a140 authored by Jishnu Bhattacharya on 14 April 2024, 14:51:22 UTC, committed by GitHub on 14 April 2024, 14:51:22 UTC
The idea is that functions like `zeros` are essentially constructing a
container and filling it with a value. `similar` seems perfectly placed
to construct such a container, so we may accept arbitrary axes in
`zeros` as long as there's a corresponding `similar` method that is
defined for the axes. Packages therefore would only need to define
`similar`, and would get `zeros`/`ones` and `fill` for free. For
example, the following will work after this:
```julia
julia> using StaticArrays

julia> zeros(SOneTo(2), 2)
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> zeros(SOneTo(2), Base.OneTo(2))
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0
```
Neither of these work on the current master, as `StaticArrays` doesn't
define `zeros` for these combinations, even though it does define
`similar`. One may argue for these methods to be added to
`StaticArrays`, but this seems to be adding redundancy.

The flip side is that `OffsetArrays` defines exactly these methods, so
adding them to `Base` would break precompilation for the package.
However, `OffsetArrays` really shouldn't be defining these methods, as
this is type-piracy. The methods may be version-limited in
`OffsetArrays` if this PR is merged.

On the face of it, `trues` and `falses` should also work similarly, but
currently these seem to be bypassing `similar` and constructing a
`BitArray` explicitly. I have not added the corresponding methods for
these functions, but they may be added as well.
1 parent b9aeafa
History
Tip revision: 8d577ab3f24212a9762ad6673e8a8fb0ecd5a140 authored by Jishnu Bhattacharya on 14 April 2024, 14:51:22 UTC
`zeros`/`ones`/`fill` may accept arbitrary axes that are supported by `similar` (#53965)
Tip revision: 8d577ab
File Mode Size
.devcontainer
.github
base
cli
contrib
deps
doc
etc
src
stdlib
test
.buildkite-external-version -rw-r--r-- 5 bytes
.clang-format -rw-r--r-- 3.3 KB
.clangd -rw-r--r-- 114 bytes
.codecov.yml -rw-r--r-- 52 bytes
.git-blame-ignore-revs -rw-r--r-- 371 bytes
.gitattributes -rw-r--r-- 65 bytes
.gitignore -rw-r--r-- 571 bytes
.mailmap -rw-r--r-- 12.7 KB
CITATION.bib -rw-r--r-- 513 bytes
CITATION.cff -rw-r--r-- 1012 bytes
CONTRIBUTING.md -rw-r--r-- 23.4 KB
HISTORY.md -rw-r--r-- 388.0 KB
LICENSE.md -rw-r--r-- 1.3 KB
Make.inc -rw-r--r-- 56.4 KB
Makefile -rw-r--r-- 30.4 KB
NEWS.md -rw-r--r-- 4.4 KB
README.md -rw-r--r-- 7.4 KB
THIRDPARTY.md -rw-r--r-- 3.9 KB
VERSION -rw-r--r-- 11 bytes
julia.spdx.json -rw-r--r-- 37.8 KB
pkgimage.mk -rw-r--r-- 1.4 KB
sysimage.mk -rw-r--r-- 4.2 KB
typos.toml -rw-r--r-- 78 bytes

README.md

back to top