Skip to content

Commit

Permalink
Allow f(x) = x style definitions with @check/@composed
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
Seelengrab committed Apr 13, 2024
1 parent ffc9ab2 commit 0533866
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ macro check(args...)
check_func(func, opts)
elseif isexpr(func, :call)
check_call(func, opts)
elseif isexpr(func, Symbol("->"))
elseif isexpr(func, Symbol("->")) | isexpr(func, Symbol("="), 2)
func = anon_to_func(func)
check_func(func, opts)
else
Expand Down Expand Up @@ -421,7 +421,7 @@ ask_number = @composed my_func(text, num)
"""
macro composed(e::Expr)
isfunc = isexpr(e, :function, 2)
isanon = isexpr(e, Symbol("->"), 2)
isanon = isexpr(e, Symbol("->"), 2) | isexpr(e, Symbol("="), 2)
iscall = isexpr(e, :call)
(isfunc | isanon | iscall) || throw(ArgumentError("Given expression is not a call or an (anonymous) function definition!"))

Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,21 @@ const verb = VERSION.major == 1 && VERSION.minor < 11
preexisting = Data.Integers{Int8}()
@testset "Single Arg, No Comma" begin
@check (a=Data.Integers{Int8}()) -> a isa Int8
@check (a=Data.Integers{Int16}()) = a isa Int16
end
@testset "Single Arg, With Comma" begin
@check (a=Data.Integers{Int8}(),) -> a isa Int8
@check (a=Data.Integers{Int16}(),) = a isa Int16
end
@testset "Multi-Arg" begin
@check (a=Data.Integers{Int8}(),b=preexisting) -> a isa Int8 && b isa Int8
@check (a=Data.Integers{Int16}(),b=preexisting) -> a isa Int16 && b isa Int8
end
@testset "Named Anonymous" begin
@check named_prop(a=Data.Integers{Int8}(),b=preexisting) -> a isa Integer && b isa Integer
@check named_prop(Data.Integers{Int8}(),preexisting)
@check inlinedef(a=Data.Floats()) = a isa AbstractFloat
@check inlinedef(Data.Floats{Float16}())
end
end
end
Expand Down

0 comments on commit 0533866

Please sign in to comment.