From 325c5542d1dda2074f4ad591bb803893dbc3b572 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 21 May 2025 10:42:43 -0400 Subject: [PATCH] Define get and getindex --- Project.toml | 2 +- src/backend_types.jl | 5 +++++ test/Project.toml | 1 + test/test_basics.jl | 22 ++++++++++++++++------ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 6e030ef..e512743 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BackendSelection" uuid = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5" authors = ["ITensor developers and contributors"] -version = "0.1.5" +version = "0.1.6" [compat] julia = "1.10" diff --git a/src/backend_types.jl b/src/backend_types.jl index f39d03b..217e265 100644 --- a/src/backend_types.jl +++ b/src/backend_types.jl @@ -30,8 +30,13 @@ function generate_backend_type_expr(type::Symbol) backend_string(::$type{Back}) where {Back} = string(Back) parameters(backend::$type) = getfield(backend, :kwargs) + # NamedTuple interface functions. Base.getproperty(backend::$type, name::Symbol) = parameters(backend)[name] Base.propertynames(backend::$type) = propertynames(parameters(backend)) + Base.getindex(backend::$type, name::Symbol) = parameters(backend)[name] + function Base.get(backend::$type, name::Symbol, default) + get(parameters(backend), name, default) + end function Base.show(io::IO, backend::$type) return print( diff --git a/test/Project.toml b/test/Project.toml index b80f121..9e6585e 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,6 +4,7 @@ BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a" [compat] Aqua = "0.8" diff --git a/test/test_basics.jl b/test/test_basics.jl index 30a9271..2245155 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1,24 +1,34 @@ using BackendSelection: BackendSelection, @Algorithm_str, @Backend_str, Algorithm, Backend using Test: @test, @testset +using TestExtras: @constinferred @testset "BackendSelection" begin for type in (Algorithm, Backend) @testset "$type" begin + # Note: constructing from a string is not type stable. @test type("backend") isa type{:backend} - @test type(:backend) isa type{:backend} - backend = type("backend"; x=2, y=3) + @test @constinferred(type(:backend)) isa type{:backend} + backend = @constinferred type(:backend; x=2, y=3) + @test backend === type("backend"; x=2, y=3) @test backend isa type{:backend} @test backend.x == 2 @test backend.y == 3 - @test propertynames(backend) == (:x, :y) + @test @constinferred(getproperty(backend, :x)) == 2 + @test @constinferred(getproperty(backend, :y)) == 3 + @test @constinferred(backend[:x]) == 2 + @test @constinferred(backend[:y]) == 3 + @test @constinferred(get(backend, :x, "default")) == 2 + @test @constinferred(get(backend, :y, "default")) == 3 + @test @constinferred(get(backend, :z, "default")) == "default" + @test @constinferred(propertynames(backend)) == (:x, :y) (; x, y) = backend @test x == 2 @test y == 3 - @test BackendSelection.parameters(backend) === (; x=2, y=3) + @test @constinferred(BackendSelection.parameters(backend)) === (; x=2, y=3) end end # Macro syntax. - @test Algorithm"backend"(; x=2, y=3) === Algorithm("backend"; x=2, y=3) - @test Backend"backend"(; x=2, y=3) === Backend("backend"; x=2, y=3) + @test @constinferred(Algorithm"backend"(; x=2, y=3)) === Algorithm("backend"; x=2, y=3) + @test @constinferred(Backend"backend"(; x=2, y=3)) === Backend("backend"; x=2, y=3) @test isnothing(show(Algorithm(""))) end