Julia: Cannot define types inside a @testset

Created on 20 Sep 2017  ·  8Comments  ·  Source: JuliaLang/julia

If I do:

julia> begin
           struct foo
               bar
           end
       end

Everything is fine, however if I make this a @testset, I get:

julia> @testset "Baz" begin
           struct foo
               bar
           end
       end
ERROR: error compiling anonymous: type definition not allowed inside a local scope

It would be nice to be able to define types within a testset.

testsystem

Most helpful comment

But also, unrelated to the Module idea.. how does the include mechanism work? You can include a file from within a function or within a @testset, and the file you include can do top-level stuff like define structs. Can we use whatever mechanism it's using?

All 8 comments

It would probably be fine if the @testset macro hoisted the type definition out of its scope and just evaled it in global scope (once, presumably).

Yes, the only point of subtlety is that I would like the type definition itself to happen within the test set error handling domain, as I want to test things like @io struct Foo for StructIO

Could the testset define a module? Then the global runtest.jl scope would not be polluted.

Have there been any more thoughts on this since last year? I agree this would be really nice.

In particular, @testsets defining a module seems nice. Are there any problems that would come from that?

In particular, @testsets defining a module seems nice.

Don't you then have to using MyPackage, Test, etc for each @testset?

ah good point. Thanks.

Well, if it's not _entirely_ crazy, the inner, automatically created module could be set to a deepcopy of the outer module, so that it'd just be like creating a new scope. Modules don't currently have a deepcopy function defined, but it's not hard to define one. I know copying/embedding modules like this isn't something that's usually done in normal julia, but it's something I've putzed with before.

But also, unrelated to the Module idea.. how does the include mechanism work? You can include a file from within a function or within a @testset, and the file you include can do top-level stuff like define structs. Can we use whatever mechanism it's using?

Don't you then have to using MyPackage, Test, etc for each @testset?

IMO this is where a import ..: * would be really useful. Our scoping rules implicitly do this, I feel we should be able to do this at the module level as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tkoolen picture tkoolen  ·  3Comments

arshpreetsingh picture arshpreetsingh  ·  3Comments

sbromberger picture sbromberger  ·  3Comments

StefanKarpinski picture StefanKarpinski  ·  3Comments

wilburtownsend picture wilburtownsend  ·  3Comments