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

Enable the creation of variables using @variables X(vec...) #1365

Merged
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
5 changes: 5 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncertain if this is the correct test file, but the best one I found. happy to move elsewhere/create a new file.

Loading