From cc077d6bcab29dc54ef60d703551e9e6e23cb83d Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Mon, 9 Feb 2015 15:19:17 -0500 Subject: [PATCH] Define X_n variables on entry to function generated by nsplat/ngenerate --- src/ngenerate.jl | 5 ++++- test/runtests.jl | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ngenerate.jl b/src/ngenerate.jl index 4693f65f7..92f76e7ed 100644 --- a/src/ngenerate.jl +++ b/src/ngenerate.jl @@ -51,7 +51,10 @@ function _nsplat(prototype, body, varname, T, itersym) bodyquot = Expr(:quote, body) newbody = quote $itersym = length($varsym) - Compat.CompatCartesian.resolvesplats!($bodyquot, $varquot, $itersym) + quote + @nexprs $($itersym) (d->$($(Expr(:quote, symbol(varsym, "_d")))) = $($varquot)[d]) + $(Compat.CompatCartesian.resolvesplats!($bodyquot, $varquot, $itersym)) + end end prototype, newbody end diff --git a/test/runtests.jl b/test/runtests.jl index 8d812eb9a..26d13115a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -80,3 +80,16 @@ end @test size(bitrand(MersenneTwister(), 3, 4)) == (3, 4) @test size(bitrand(MersenneTwister(), (3, 4))) == (3, 4) @test rand(Bool) in [false, true] + +module CartesianTest + using Base.Cartesian, Compat + @ngenerate N NTuple{N,Int} function f(X::NTuple{N,Int}...) + @ncall N tuple X + end +end + +@test CartesianTest.f(1) == (1,) +@test CartesianTest.f(1,2) == (1,2) +@test CartesianTest.f(1,2,3) == (1,2,3) +@test CartesianTest.f(1,2,3,4) == (1,2,3,4) +@test CartesianTest.f(1,2,3,4,5) == (1,2,3,4,5)