# Make struct definition return the defined type

**URL:** https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741
**Category:** Internals & Design
**Tags:** proposal
**Created:** [April 4, 2019, 11:33am UTC](https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741 "2019-04-04T11:33:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [April 4, 2019, 11:33am UTC](https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741/1 "2019-04-04T11:33:58Z")

</div>

Currently a struct definition returns `nothing`:

```julia
julia> x = struct Foo end

julia> x === nothing
true

```

How about it returning the defined type instead?

```julia
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:

```julia

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:

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

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

```

Or even

```julia
Foo = @eval struct $(gensym())
           field1
           field2
       end

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 4, 2019, 11:57am UTC](https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741/2 "2019-04-04T11:57:00Z")

</div>

> [@jw3126](#):
>
> Now we can simply edit the above without restarting julia and it will work:

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

> <https://github.com/JuliaLang/julia/pull/22721>
>
> Several of us have found \[Revise.jl\](https://github.com/timholy/Revise.jl) to be… a significant productivity booster. In principle, it's now possible to keep a Julia session open for a week or more, at which point one of the main negatives of Julia---the cost of JITting your, e.g., plotting package---becomes a non-issue.
> 
> However, there are two events which prevent this from being commonplace. One is method-deletion (#20048) and the other is type redefinition (https://github.com/timholy/Revise.jl/issues/18). It occurred to me that type redefinition may not be quite as nasty a problem as I've thought. A demo with this PR:
> \`\`\`julia
> julia\> struct Mine
> arg
> end
> 
> julia\> a = Mine(1)
> Mine(1)
> 
> julia\> foo(val::Mine) = val.arg
> foo (generic function with 1 method)
> 
> julia\> Base.shunt\_binding(Main, :Mine)
> Mine#1
> 
> julia\> a
> Mine#1(1)
> 
> julia\> foo(a)
> 1
> 
> julia\> struct Mine{T}
> arg::T
> end
> 
> julia\> x = Mine(3)
> Mine{Int64}(3)
> \`\`\`
> 
> The only catch I'm aware of is:
> \`\`\`julia
> julia\> foo(x)
> ERROR: MethodError: no method matching foo(::Mine{Int64})
> Closest candidates are:
> foo(::Mine#1) at REPL\[3\]:1
> \`\`\`
> 
> However, I wonder if this might be solvable via \`methodswith\` \*plus\* maintaining a cache of the source-code expressions so that they can be re-evaluated. (Revise would like that anyway, since it re-parses and caches every source file so that it can detect diffs. This adds considerably to the package load time, but caching the Exprs to the \`.ji\` file seems both cheap and effective.)

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [April 4, 2019, 1:40pm UTC](https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741/3 "2019-04-04T13:40:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [April 4, 2019, 3:04pm UTC](https://discourse.julialang.org/t/make-struct-definition-return-the-defined-type/22741/4 "2019-04-04T15:04:29Z")

</div>

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.
