Skip to content

Remove generic AbstractArray fallbacks for position #26

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

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TypeParameterAccessors"
uuid = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.2"
version = "0.3.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 0 additions & 1 deletion src/TypeParameterAccessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ include("type_parameters.jl")
include("ndims.jl")
include("base/abstractarray.jl")
include("base/similartype.jl")
include("base/array.jl")
include("base/linearalgebra.jl")

end
23 changes: 11 additions & 12 deletions src/base/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
position(::Type{AbstractArray}, ::typeof(eltype)) = Position(1)
position(::Type{AbstractArray}, ::typeof(ndims)) = Position(2)
default_type_parameters(::Type{AbstractArray}) = (Float64, 1)

Check warning on line 3 in src/base/abstractarray.jl

View check run for this annotation

Codecov / codecov/patch

src/base/abstractarray.jl#L3

Added line #L3 was not covered by tests

position(::Type{<:Array}, ::typeof(eltype)) = Position(1)
position(::Type{<:Array}, ::typeof(ndims)) = Position(2)
default_type_parameters(::Type{<:Array}) = (Float64, 1)

position(::Type{<:BitArray}, ::typeof(ndims)) = Position(1)
default_type_parameters(::Type{<:BitArray}) = (1,)

Check warning on line 10 in src/base/abstractarray.jl

View check run for this annotation

Codecov / codecov/patch

src/base/abstractarray.jl#L9-L10

Added lines #L9 - L10 were not covered by tests

struct Self end
position(a, ::Self) = Position(0)
position(::Type{T}, ::Self) where {T} = Position(0)
Expand Down Expand Up @@ -70,18 +81,6 @@
return set_type_parameters(type, eltype, param)
end

# These are generic fallback definitions. By convention,
# this is very commonly true of `AbstractArray` subtypes
# but it may not be correct, but it is very convenient
# to define this to make more operations "just work"
# on most AbstractArrays.
# TODO: evaluate if this is actually the case, and weigh up the benefits of ease of use
# against not having a helpful error thrown
position(type::Type{<:AbstractArray}, ::typeof(eltype)) = Position(1)
position(type::Type{<:AbstractArray}, ::typeof(ndims)) = Position(2)

default_type_parameters(::Type{<:AbstractArray}) = (Float64, 1)

for wrapper in [:PermutedDimsArray, :(Base.ReshapedArray), :SubArray]
@eval begin
position(type::Type{<:$wrapper}, ::typeof(eltype)) = Position(1)
Expand Down
2 changes: 0 additions & 2 deletions src/base/array.jl

This file was deleted.

6 changes: 5 additions & 1 deletion src/type_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ function position end
position(object, name) = position(typeof(object), name)
position(::Type, pos::Int) = Position(pos)
position(::Type, pos::Position) = pos
position(type::Type, pos) = throw(MethodError(position, (type, pos)))
function position(type::Type, name)
base_type = unspecify_type_parameters(type)
base_type === type && error("`position` not defined for $type and $name.")
return position(base_type, name)
end

function positions(::Type{T}, pos::Tuple) where {T}
return ntuple(length(pos)) do i
Expand Down
File renamed without changes.
File renamed without changes.
28 changes: 0 additions & 28 deletions test/utils/test_inferred.jl

This file was deleted.