How do I write a @generated inner constructor?


#1

I’m trying to create a type with an @generated inner constructor, but it seems I’m doing something wrong? Or maybe generated inner constructors are not permitted?

Here’s my attempt:

julia> immutable T
         @generated T() = :( new() )
       end

julia> T()
ERROR: UndefVarError: new not defined
Stacktrace:
 [1] T() at ./REPL[10]:2

#2

What are you trying to achieve?


#3

I don’t think it’s currently easy to do. You can generate the Expr(:new) manually but maybe we can fix the generated function lowering for inner constructors…


#4

Expr(:new) gave a segfault when I tried it. Expr(:call, :new) gave the same error message as :( new() ).

The reason I would like a generated inner constructor is that I want to do some sanity checks on type parameters, and throw an exception if there’s a problem. I had expected to be able to do this without any performance penalty, since type parameters are known constants when the method is compiled. But it turns out to be more complicated than I first thought. Instructions related to garbage collection sometimes get added even though they are not needed. (Apparently the compiler does not always realize when the garbage collected objects are constants that get folded away.) A generated function would not have this problem.


#5

… Well, you don’t just do Expr(:new). You need to fill in the argument. Check code_lowered of a constructor to see what you should generate.

julia> type A
       A() = new()
       end

julia> @code_lowered A()
CodeInfo(:(begin
        nothing
        return $(Expr(:new, :(Main.A)))
    end))

This is also mentioned in the dev doc. Note that this is the internal AST so it can break more easily than the surface AST.


#6

Just use dispatch to other functions that do the type checks for you. Those will receive the same optimizations for known types. Cf. https://github.com/JuliaLang/julia/blob/003a14fd215954581147df49d37e7c05fb826771/base/subarray.jl#L15


#7

Awesome! Thanks.


#8

I had tried this, but it did not work when I passed the type parameters as arguments to the checking function. (Apparently the types themselves become garbage collected objects at that point.) The example you give instead infers the type parameters anew from the the types of passed-through arguments. I can see why that should work much better. I will try this!


#9

It should also work when passing types to the checking function. The key is to put your logic into the dispatch table instead of inside a single method. Dispatching on ::Type{…} isn’t really any different than dispatching on concrete types… although invariance can make it a little harder to express.


#10

I put the logic in a @generated checking function. Equivalent to putting it in the dispatch table in a way, but gives more flexibility. Works great! Thanks for the tip.