Skip to content

Commit ca6b2c0

Browse files
authored
Remove generic AbstractArray fallbacks for position (#26)
1 parent d97e129 commit ca6b2c0

File tree

8 files changed

+17
-45
lines changed

8 files changed

+17
-45
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TypeParameterAccessors"
22
uuid = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.2"
4+
version = "0.3.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/TypeParameterAccessors.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ include("type_parameters.jl")
1818
include("ndims.jl")
1919
include("base/abstractarray.jl")
2020
include("base/similartype.jl")
21-
include("base/array.jl")
2221
include("base/linearalgebra.jl")
2322

2423
end

src/base/abstractarray.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
position(::Type{AbstractArray}, ::typeof(eltype)) = Position(1)
2+
position(::Type{AbstractArray}, ::typeof(ndims)) = Position(2)
3+
default_type_parameters(::Type{AbstractArray}) = (Float64, 1)
4+
5+
position(::Type{<:Array}, ::typeof(eltype)) = Position(1)
6+
position(::Type{<:Array}, ::typeof(ndims)) = Position(2)
7+
default_type_parameters(::Type{<:Array}) = (Float64, 1)
8+
9+
position(::Type{<:BitArray}, ::typeof(ndims)) = Position(1)
10+
default_type_parameters(::Type{<:BitArray}) = (1,)
11+
112
struct Self end
213
position(a, ::Self) = Position(0)
314
position(::Type{T}, ::Self) where {T} = Position(0)
@@ -70,18 +81,6 @@ end
7081
return set_type_parameters(type, eltype, param)
7182
end
7283

73-
# These are generic fallback definitions. By convention,
74-
# this is very commonly true of `AbstractArray` subtypes
75-
# but it may not be correct, but it is very convenient
76-
# to define this to make more operations "just work"
77-
# on most AbstractArrays.
78-
# TODO: evaluate if this is actually the case, and weigh up the benefits of ease of use
79-
# against not having a helpful error thrown
80-
position(type::Type{<:AbstractArray}, ::typeof(eltype)) = Position(1)
81-
position(type::Type{<:AbstractArray}, ::typeof(ndims)) = Position(2)
82-
83-
default_type_parameters(::Type{<:AbstractArray}) = (Float64, 1)
84-
8584
for wrapper in [:PermutedDimsArray, :(Base.ReshapedArray), :SubArray]
8685
@eval begin
8786
position(type::Type{<:$wrapper}, ::typeof(eltype)) = Position(1)

src/base/array.jl

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/type_parameters.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ function position end
2727
position(object, name) = position(typeof(object), name)
2828
position(::Type, pos::Int) = Position(pos)
2929
position(::Type, pos::Position) = pos
30-
position(type::Type, pos) = throw(MethodError(position, (type, pos)))
30+
function position(type::Type, name)
31+
base_type = unspecify_type_parameters(type)
32+
base_type === type && error("`position` not defined for $type and $name.")
33+
return position(base_type, name)
34+
end
3135

3236
function positions(::Type{T}, pos::Tuple) where {T}
3337
return ntuple(length(pos)) do i
File renamed without changes.
File renamed without changes.

test/utils/test_inferred.jl

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)