Skip to content
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

All the getindex implementations should use view, not slice #26

Open
jpivarski opened this issue Aug 31, 2023 · 3 comments
Open

All the getindex implementations should use view, not slice #26

jpivarski opened this issue Aug 31, 2023 · 3 comments

Comments

@jpivarski
Copy link
Member

I just noticed that slices copy! They were supposed to be views.

@Moelf
Copy link
Member

Moelf commented Aug 31, 2023

hmm, I'm pretty sure you're not supposed to make a default slice "view"

JuliaLang/julia#47078

@jpivarski
Copy link
Member Author

This is what I'm seeing, with the concrete Vector class, at least:

julia> original = Vector([1.1, 2.2, 3.3, 4.4, 5.5])
5-element Vector{Float64}:
 1.1
 2.2
 3.3
 4.4
 5.5

julia> sliced = original[2:4]
3-element Vector{Float64}:
 2.2
 3.3
 4.4

julia> viewed = view(original, 2:4)
3-element view(::Vector{Float64}, 2:4) with eltype Float64:
 2.2
 3.3
 4.4

julia> original[3] = 9999
9999

julia> sliced
3-element Vector{Float64}:
 2.2
 3.3
 4.4

julia> viewed
3-element view(::Vector{Float64}, 2:4) with eltype Float64:
    2.2
 9999.0
    4.4

Oh, but maybe this means that the AwkwardArray getindex is correct because it copies, and I should additionally provide a view override.

(In Python, Content instances are considered immutable, so they view everywhere. But AwkwardArray.jl implements Base.push! and Base.append! on its Content instances, so perhaps they should not be presumed immutable. It's also less necessary to require immutability, since typical usage in Julia will be to iterate over arrays, rather than call functions that replace arrays with new arrays.)

@Moelf
Copy link
Member

Moelf commented Aug 31, 2023

you don't need to add view override yourself. All AbstractArray which has getindex() correctly implemented has view() by default.

You can use @view on one expression, or @views on a whole lexical code block to recover Numpy default slicing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants