Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify @withmetadata to filter strings in field definitions #77

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v0.4.2 - 2024-08-11

- `@withmetadata` now supports inline docstrings for struct fields

## v0.4.1 - 2024-08-11

- Minor documentation improvements.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumSymbolics"
uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae"
authors = ["QuantumSymbolics.jl contributors"]
version = "0.4.1"
version = "0.4.2"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
5 changes: 3 additions & 2 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using SymbolicUtils
import SymbolicUtils: Symbolic,_isone,flatten_term,isnotflat,Chain,Fixpoint,Prewalk,sorted_arguments
using TermInterface
import TermInterface: isexpr,head,iscall,children,operation,arguments,metadata,maketerm
import MacroTools
import MacroTools: namify, @capture

using LinearAlgebra
Expand Down Expand Up @@ -70,11 +71,11 @@ macro withmetadata(strct)
ex = quote $strct end
if @capture(ex, (struct T_{params__} fields__ end) | (struct T_{params__} <: A_ fields__ end))
struct_name = namify(T)
args = (namify(i) for i in fields)
args = (namify(i) for i in fields if !MacroTools.isexpr(i, String, :string))
constructor = :($struct_name{S}($(args...)) where S = new{S}($((args..., :(Metadata()))...)))
elseif @capture(ex, struct T_ fields__ end)
struct_name = namify(T)
args = (namify(i) for i in fields)
args = (namify(i) for i in fields if !MacroTools.isexpr(i, String, :string))
constructor = :($struct_name($(args...)) = new($((args..., :(Metadata()))...)))
else @capture(ex, struct T_ end)
struct_name = namify(T)
Expand Down
36 changes: 36 additions & 0 deletions test/test_metadata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@testitem "Test metadata decoration" begin
using QuantumSymbolics: Metadata, @withmetadata

@withmetadata struct Foo1
a::Int
end
@test Foo1(2).metadata isa Metadata

@withmetadata struct Foo2
"hi"
a::Int
end
@test Foo2(2).metadata isa Metadata

@withmetadata struct Foo3{T<:Int}
"hi"
a::T
"hi"
b::T
end
@test Foo3{Int}(2, 3).metadata isa Metadata

@withmetadata struct Foo4{T<:Int} <: Integer
a::T
b::T
end
@test Foo4{Int}(2, 3).metadata isa Metadata

@withmetadata struct Foo5 <: Integer
a
end
@test Foo5(2).metadata isa Metadata

@withmetadata struct Foo6 <: Integer end
@test Foo6().metadata isa Metadata
end
Loading