Skip to content

Commit 4441dc6

Browse files
Export fieldindex (#58119)
At present, we export `Base.fieldname`, which maps a field's index to its symbol. Although we have the inverse function, `Base.fieldindex`, which maps a field's symbol to its index, defined and documented, we currently do not export it. This PR changes this and exports it to enhance the consistency and completeness of the field introspection functions. Additionally, it is unlikely that the function will undergo fundamental changes in the future, so exporting it should not pose a significant maintenance burden. The interface is likely fully defined by the following two properties. ```julia @test fieldname(Some{Int}, Base.fieldindex(Some{Int}, :value)) === :value @test Base.fieldindex(Some{Int}, fieldname(Some{Int}, 1)) === 1 ``` Besides doing the `export`, this PR adapts the documentation and adds test cases. Fixes #58092 Thanks to @nsajko for the support in covering all artifacts.
1 parent ead23db commit 4441dc6

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Build system changes
2424
New library functions
2525
---------------------
2626

27+
* Exporting function `fieldindex` to get the index of a struct's field ([#58119]).
28+
2729
New library features
2830
--------------------
2931

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ export
807807
fieldoffset,
808808
fieldname,
809809
fieldnames,
810+
fieldindex,
810811
fieldcount,
811812
fieldtypes,
812813
hasfield,

base/runtime_internals.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ String
10571057
fieldtype
10581058

10591059
"""
1060-
Base.fieldindex(T, name::Symbol, err:Bool=true)
1060+
fieldindex(T, name::Symbol, err:Bool=true)
10611061
10621062
Get the index of a named field, throwing an error if the field does not exist (when err==true)
10631063
or returning 0 (when err==false).
@@ -1069,14 +1069,20 @@ julia> struct Foo
10691069
y::String
10701070
end
10711071
1072-
julia> Base.fieldindex(Foo, :z)
1072+
julia> fieldindex(Foo, :y)
1073+
2
1074+
1075+
julia> fieldindex(Foo, :z)
10731076
ERROR: FieldError: type Foo has no field `z`, available fields: `x`, `y`
10741077
Stacktrace:
10751078
[...]
10761079
1077-
julia> Base.fieldindex(Foo, :z, false)
1080+
julia> fieldindex(Foo, :z, false)
10781081
0
10791082
```
1083+
1084+
!!! compat "Julia 1.13"
1085+
This function is exported as of Julia 1.13.
10801086
"""
10811087
function fieldindex(T::DataType, name::Symbol, err::Bool=true)
10821088
return err ? _fieldindex_maythrow(T, name) : _fieldindex_nothrow(T, name)

doc/src/base/base.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Base.isstructtype
207207
Base.nameof(::DataType)
208208
Base.fieldnames
209209
Base.fieldname
210+
Base.fieldindex
210211
Core.fieldtype
211212
Base.fieldtypes
212213
Base.fieldcount

test/reflection.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ tlayout = TLayout(5,7,11)
361361
@test fieldtype(Union{Tuple{Char},Tuple{Char,Char}},2) === Char
362362
@test_throws BoundsError fieldtype(Union{Tuple{Char},Tuple{Char,Char}},3)
363363

364+
@test [fieldindex(TLayout, i) for i = (:x, :y, :z)] == [1, 2, 3]
365+
@test fieldname(TLayout, fieldindex(TLayout, :z)) === :z
366+
@test fieldindex(TLayout, fieldname(TLayout, 3)) === 3
367+
364368
@test fieldnames(NTuple{3, Int}) == ntuple(i -> fieldname(NTuple{3, Int}, i), 3) == (1, 2, 3)
365369
@test_throws ArgumentError fieldnames(Union{})
366370
@test_throws BoundsError fieldname(NTuple{3, Int}, 0)

0 commit comments

Comments
 (0)