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

Assign type parameters when declaring struct acset type #120

Merged
merged 2 commits into from
Feb 28, 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
14 changes: 12 additions & 2 deletions src/DenseACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,25 @@ macro acset_type(head)
Expr(:(<:), h, p) => (h,p)
_ => (head, GlobalRef(DenseACSets, :StructACSet))
end
name, schema, idx_args = @match head begin
head′, type_params = @match head begin
Expr(:curly, h, ps...) => (h, ps)
_ => (head, [])
end
name, schema, idx_args = @match head′ begin
Expr(:call, name, schema, idx_args...) => (name, schema, idx_args)
_ => error("Unsupported head for @acset_type")
end
name′ = name
assgn = if !isempty(type_params)
name = gensym(name)
:(const $(esc(name′))=$(esc(name)){$(type_params...)})
end
quote
$(esc(:eval))($(GlobalRef(DenseACSets, :struct_acset))(
$(Expr(:quote, name)), $(Expr(:quote, parent)), $(esc(schema));
$((esc(arg) for arg in idx_args)...)))
Core.@__doc__ $(esc(name))
$assgn
Core.@__doc__ $(esc(name′))
end
end

Expand Down
17 changes: 16 additions & 1 deletion test/ACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,19 @@ datcompdyn = DynamicACSet(datcomp)

@test_throws Exception incident(datcompdyn, 1, (:h,:g))

end
# Test @acset_type with type parameters
#--------------------------------------
# Single
@acset_type IntLabeledSet(SchLabeledSet, index=[:label]){Int}
@test isempty(IntLabeledSet())

# Multiple
SchLabeledDecGraph′ = BasicSchema([:E,:V], [(:src,:E,:V),(:tgt,:E,:V)],
[:X, :Y], [(:dec,:E,:X),(:label,:V,:Y)])
"""Example Docstring"""
@acset_type SymSymDecGraph(SchLabeledDecGraph′){Symbol,Symbol}
@test strip(string(@doc SymSymDecGraph)) == """Example Docstring"""

@acset SymSymDecGraph begin V=1; E=1; src=1; tgt=1; dec=[:a]; label=[:b] end

end # module
Loading