<p dir="auto">I found some discussion about this in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="57128691" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/10146" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/10146/hovercard?comment_id=73742855&amp;comment_type=issue_comment" href="https://github.com/JuliaLang/julia/issues/10146#issuecomment-73742855">#10146 (comment)</a>, but no dedicated issue.</p>
<p dir="auto">What I would expect this to look like:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="type Foo
    a::Int32
    b::Int32
    c::Int32

    function Foo()
        return new(c = 123) # a and b undefinied
    end
end"><pre class="notranslate">type Foo
    a<span class="pl-k">::</span><span class="pl-c1">Int32</span>
    b<span class="pl-k">::</span><span class="pl-c1">Int32</span>
    c<span class="pl-k">::</span><span class="pl-c1">Int32</span>

    <span class="pl-k">function</span> <span class="pl-en">Foo</span>()
        <span class="pl-k">return</span> <span class="pl-c1">new</span>(c <span class="pl-k">=</span> <span class="pl-c1">123</span>) <span class="pl-c"><span class="pl-c">#</span> a and b undefinied</span>
    <span class="pl-k">end</span>
<span class="pl-k">end</span></pre></div>
<h1 dir="auto">The Problem</h1>
<p dir="auto">Currently, when defining <a href="https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods-1" rel="nofollow">inner constructors</a>, we can decide to <a href="https://docs.julialang.org/en/v1/manual/constructors/#Incomplete-Initialization-1" rel="nofollow">incompletely initialize</a> a structure simply by passing to <code class="notranslate">new(...)</code> fewer values than the number of fields:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct Incomplete
    x1
    x2
    x3
    ...
    x719
    Incomplete(x1, x2, x3, x4) = new(x1, x2, x3, x4) # x5, ..., x719 are uninitialized
end"><pre class="notranslate"><span class="pl-k">struct</span> Incomplete
    x1
    x2
    x3
    <span class="pl-k">...</span>
    x719
    <span class="pl-en">Incomplete</span>(x1, x2, x3, x4) <span class="pl-k">=</span> <span class="pl-c1">new</span>(x1, x2, x3, x4) <span class="pl-c"><span class="pl-c">#</span> x5, ..., x719 are uninitialized</span>
<span class="pl-k">end</span></pre></div>
<p dir="auto">Since the call to <code class="notranslate">new(...)</code> accepts only positional arguments, this has the obvious limitation that we can only initialize a prefix of all the fields and leave the remaining ones uninitialized. It is impossible to initialize only <code class="notranslate">x1</code>, <code class="notranslate">x42</code>, <code class="notranslate">x470</code> and <code class="notranslate">x666</code>.</p>
<h1 dir="auto">The Proposal</h1>
<p dir="auto">My proposal is to allow <code class="notranslate">new(...)</code> to accept keyword arguments, so that the following code becomes legal:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct Incomplete
    x1
    x2
    x3
    ...
    x719
    Incomplete(a, b, c, d) = new(x1=a, x42=b, x470=c, x666=d) # the other fields are uninitialized
end"><pre class="notranslate"><span class="pl-k">struct</span> Incomplete
    x1
    x2
    x3
    <span class="pl-k">...</span>
    x719
    <span class="pl-en">Incomplete</span>(a, b, c, d) <span class="pl-k">=</span> <span class="pl-c1">new</span>(x1<span class="pl-k">=</span>a, x42<span class="pl-k">=</span>b, x470<span class="pl-k">=</span>c, x666<span class="pl-k">=</span>d) <span class="pl-c"><span class="pl-c">#</span> the other fields are uninitialized</span>
<span class="pl-k">end</span></pre></div>
<h1 dir="auto">Remarks</h1>
<h2 dir="auto">Motivation</h2>
<p dir="auto">It is just useful sometimes to be able to do this. See for instance <a href="https://discourse.julialang.org/t/incomplete-initialization-control-which-fields-are-initialized-in-new/43452" rel="nofollow">this thread</a> where I asked on the forum if there was a way to achieve this with the current state of the language. There are plenty of workarounds, with more or less significant downsides (complexity-wise and performance-wise), but no direct solution.</p>
<h2 dir="auto">Backward compatibility</h2>
<p dir="auto">This should be <g-emoji class="g-emoji" alias="100" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f4af.png">💯</g-emoji>.</p>
<h2 dir="auto">Downsides</h2>
<p dir="auto">I honestly don't see any.</p>
<h2 dir="auto">Labels</h2>
<p dir="auto">design, feature, keyword arguments, types and dispatch</p>