Skip to content

Commit 704135c

Browse files
authored
Define get and getindex (#16)
1 parent 819b11f commit 704135c

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BackendSelection"
22
uuid = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.1.5"
4+
version = "0.1.6"
55

66
[compat]
77
julia = "1.10"

src/backend_types.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ function generate_backend_type_expr(type::Symbol)
3030
backend_string(::$type{Back}) where {Back} = string(Back)
3131
parameters(backend::$type) = getfield(backend, :kwargs)
3232

33+
# NamedTuple interface functions.
3334
Base.getproperty(backend::$type, name::Symbol) = parameters(backend)[name]
3435
Base.propertynames(backend::$type) = propertynames(parameters(backend))
36+
Base.getindex(backend::$type, name::Symbol) = parameters(backend)[name]
37+
function Base.get(backend::$type, name::Symbol, default)
38+
get(parameters(backend), name, default)
39+
end
3540

3641
function Base.show(io::IO, backend::$type)
3742
return print(

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
44
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
55
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
66
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7+
TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a"
78

89
[compat]
910
Aqua = "0.8"

test/test_basics.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
using BackendSelection: BackendSelection, @Algorithm_str, @Backend_str, Algorithm, Backend
22
using Test: @test, @testset
3+
using TestExtras: @constinferred
34

45
@testset "BackendSelection" begin
56
for type in (Algorithm, Backend)
67
@testset "$type" begin
8+
# Note: constructing from a string is not type stable.
79
@test type("backend") isa type{:backend}
8-
@test type(:backend) isa type{:backend}
9-
backend = type("backend"; x=2, y=3)
10+
@test @constinferred(type(:backend)) isa type{:backend}
11+
backend = @constinferred type(:backend; x=2, y=3)
12+
@test backend === type("backend"; x=2, y=3)
1013
@test backend isa type{:backend}
1114
@test backend.x == 2
1215
@test backend.y == 3
13-
@test propertynames(backend) == (:x, :y)
16+
@test @constinferred(getproperty(backend, :x)) == 2
17+
@test @constinferred(getproperty(backend, :y)) == 3
18+
@test @constinferred(backend[:x]) == 2
19+
@test @constinferred(backend[:y]) == 3
20+
@test @constinferred(get(backend, :x, "default")) == 2
21+
@test @constinferred(get(backend, :y, "default")) == 3
22+
@test @constinferred(get(backend, :z, "default")) == "default"
23+
@test @constinferred(propertynames(backend)) == (:x, :y)
1424
(; x, y) = backend
1525
@test x == 2
1626
@test y == 3
17-
@test BackendSelection.parameters(backend) === (; x=2, y=3)
27+
@test @constinferred(BackendSelection.parameters(backend)) === (; x=2, y=3)
1828
end
1929
end
2030
# Macro syntax.
21-
@test Algorithm"backend"(; x=2, y=3) === Algorithm("backend"; x=2, y=3)
22-
@test Backend"backend"(; x=2, y=3) === Backend("backend"; x=2, y=3)
31+
@test @constinferred(Algorithm"backend"(; x=2, y=3)) === Algorithm("backend"; x=2, y=3)
32+
@test @constinferred(Backend"backend"(; x=2, y=3)) === Backend("backend"; x=2, y=3)
2333
@test isnothing(show(Algorithm("")))
2434
end

0 commit comments

Comments
 (0)