Skip to content

Commit

Permalink
Optional typed wrapper constructor where type parameters are provided
Browse files Browse the repository at this point in the history
Weaken
  • Loading branch information
Kris Brown committed Dec 12, 2024
1 parent ee14b2b commit cc44137
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/syntax/TheoryInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ function wrapper(name::Symbol, t::GAT, mod)
Xs = map(Ts) do s
:($(GlobalRef($(TheoryInterface), :impl_type))(x, $($(name)), $($(Meta.quot)(s))))
end
XTs = map(zip(Ts,Xs)) do (T,X)
:($X <: $T || error("Mismatch $($($(Meta.quot)(T))): $($X)$($T)"))
end
esc(quote
# Catch any potential docs above the macro call
const $(doctarget) = nothing
Expand All @@ -337,6 +340,14 @@ function wrapper(name::Symbol, t::GAT, mod)
# Declare the wrapper struct
struct $n{$(Ts...)} <: $abs
val::Any
function $n{$(Ts...)}(x::Any) where {$(Ts...)}
$($(GlobalRef(TheoryInterface, :implements)))(x, $($name)) || error(
"Invalid $($($(name))) model: $x")
$(XTs...)
# CHECK THAT THE GIVEN PARAMETERS MATCH Xs
new{$(Ts...)}(x)
end

function $n(x::Any)
$($(GlobalRef(TheoryInterface, :implements)))(x, $($name)) || error(
"Invalid $($($(name))) model: $x")
Expand Down
2 changes: 2 additions & 0 deletions test/models/ModelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ ThCategory.Meta.@wrapper Cat2 <: MyAbsType
ThCategory.Meta.@typed_wrapper TCat

c = TCat(FinSetC())
@test c == TCat{Int,Vector{Int}}(FinSetC())
@test_throws ErrorException TCat{Bool,Symbol}(FinSetC()) # Ob: Int ⊄ Bool
@test c isa TCat{Int, Vector{Int}}
@test id(c, 2) == [1,2]

Expand Down

0 comments on commit cc44137

Please sign in to comment.