From 94d8f183a7fb55d8ea16193e9720aef00c2e9c06 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 12 Feb 2026 09:34:19 -0700 Subject: [PATCH] Constrain getindex arg type in @selectors macro to fix downstream ambiguities (#40) The @selectors macro generated `getindex(x::T, arg)` with unconstrained `arg`, causing method ambiguities in packages that define getindex on AbstractArray with specific index types (e.g. Reactant.jl). Co-Authored-By: Claude Opus 4.6 --- src/selectors.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/selectors.jl b/src/selectors.jl index b63ff0c..c626dd8 100644 --- a/src/selectors.jl +++ b/src/selectors.jl @@ -55,6 +55,9 @@ const KeyInd = Union{AbstractString, Symbol} const Inds = Union{AbstractVector{<:KeyInd}, NTuple{N, <:KeyInd} where {N}, AbstractVector{<:Integer}, NTuple{N, <:Integer} where {N}} +# Union of all valid single-argument selector index types +const SelectorIndex = Union{KeyInd, Integer, Colon, Inds} + function _getindex_array(x, key::Union{KeyInd, Integer}) values = List() StructUtils.applyeach(x) do _, item @@ -215,7 +218,7 @@ end # convenience macro for defining high-level getindex/getproperty methods macro selectors(T) esc(quote - Base.getindex(x::$T, arg) = StructUtils.Selectors._getindex(x, arg) + Base.getindex(x::$T, arg::StructUtils.Selectors.SelectorIndex) = StructUtils.Selectors._getindex(x, arg) Base.getindex(x::$T, ::Colon, arg) = StructUtils.Selectors._getindex(x, :, arg) Base.getindex(x::$T, ::typeof(~), arg) = StructUtils.Selectors._getindex(x, ~, arg) Base.getindex(x::$T, ::typeof(~), key, val) = StructUtils.Selectors._getindex(x, ~, key, val)