Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
TorkelE committed Nov 17, 2024
1 parent 1df3744 commit 29c4526
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function _parse_vars(macroname, type, x, transform=identity)
isruntime, fname = unwrap_runtime_var(v.args[1])
call_args = map(lastunwrap_runtime_var, @view v.args[2:end])
var_name, expr = construct_vars(macroname, fname, type′, call_args, val, options, transform, isruntime)

else
var_name, expr = construct_vars(macroname, v, type′, nothing, val, options, transform, isruntime)
end
Expand Down Expand Up @@ -344,6 +345,11 @@ function construct_var(macroname, var_name, type, call_args, val, prop)
var_name, fntype = function_name_and_type(var_name)
argtypes = arg_types_from_call_args(call_args)
:($CallWithMetadata($Sym{$FnType{$argtypes, $type, $(fntype...)}}($var_name)))
# This elseif handles the special case with e.g. variables on the form
# @variables X(deps...) where deps is a vector (which length might be unknown).
elseif (call_args isa Vector) && (length(call_args) == 1) && (call_args[1] isa Expr) &&
call_args[1].head == :(...) && (length(call_args[1].args) == 1)
:($Sym{$FnType{NTuple{$length($(call_args[1].args[1])), Any}, $type}}($var_name)($value.($(call_args[1].args[1]))...))
else
:($Sym{$FnType{NTuple{$(length(call_args)), Any}, $type}}($var_name)($(map(x->:($value($x)), call_args)...)))
end
Expand Down
28 changes: 28 additions & 0 deletions test/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,31 @@ fns = @test_nowarn @variables begin
end

test_all_functions(fns)

# Tests that variables can be declared using vectors of dependants.
let
@variables x y z v w
args1 = [x]
args2 = [x, y]
args3 = [x, y, z]

v1 = only(@variables X(x))
v2 = only(@variables X(args1...))
@test isequal(v1, v2)

v1 = only(@variables X(x,y))
v2 = only(@variables X(args3[1:2]...))
@test isequal(v1, v2)

v1 = only(@variables X(x, y, z))
v2 = only(@variables X([args2; z]...))
@test isequal(v1, v2)

v1 = only(@variables X(x, y, z, v))
v2 = only(@variables X(vcat(args2, [z, v])...))
@test isequal(v1, v2)

v1 = only(@variables X(x, y, z, v, w))
v2 = only(@variables X([v for v in [args3; [v, w]]]...))
@test isequal(v1, v2)
end

0 comments on commit 29c4526

Please sign in to comment.