Make struct definition return the defined type

Currently a struct definition returns nothing:

julia> x = struct Foo end

julia> x === nothing
true

How about it returning the defined type instead?

julia> x = struct Foo end

julia> x === Foo
true

Why would this behaviour be useful? It would make the following pattern to “redefine types” more convenient:


Foo = struct Foo1
    field1
end

function doit(foo::Foo)
    foo.field1 + foo.field2
end

Now we can simply edit the above without restarting julia and it will work:

Foo = struct Foo2{T <: Number}
    field1::T
    field2::T
end

function doit(foo::Foo)
    foo.field1 + foo.field2
end

Or even

Foo = @eval struct $(gensym())
           field1
           field2
       end
1 Like

I don’t think we should introduce a language future just to work around the issues addressed by

2 Likes

I agree that one should not introduce language features just to hack around other limitations. However this one is also natural and consistent. For instance function definitions return the definied function.

It’s actually pretty reasonable for a type definition to return the type that’s defined though. The main reason we don’t do that is because printing the type in the REPL after it’s defined is a bit annoying. Returning the type would be more lispy.

1 Like