Create a struct with uninitialized fields

I ended up with the following implementation:

"""
    __new__(T, args...)
User-level version of the `new()` pseudofunction.
Can be used to construct most Julia types, including structs
without default constructors, closures, etc.
"""
@generated function __new__(T, args...)
    return Expr(:splatnew, :T, :args)
end

I did’t try it with undef initializer, but given actual arguments, it works pretty well for quite a wide range if use cases in Ghost.jl. One caution though is that since :new/:splatnew are quite low-level, Julia won’t validate this expression, so misuse may lead to segfault.

4 Likes