title: Keyword argument when instantiating a Type
body: <p>Suppose I have the following type:</p>
  
  <pre><code>type Foo
      a::Int64
      b::Int64
  end
  </code></pre>
  
  <p>I can instantiate this with</p>
  
  <pre><code>bar = Foo(1,2)
  </code></pre>
  
  <p>Is there a way to use keywords here, because in the above I have to remember that <code>a</code> is first, and <code>b</code> is second. Something like this:</p>
  
  <pre><code>bar = Foo(a=1, b=2)
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <p>The solution by <a href="https://stackoverflow.com/a/29262847/788700">spencerlyon2</a> doesn't work if called from the function:</p>
  
  <pre><code>#!/usr/bin/env julia
  
  type Foo
      a::Float64
      b::Float64
  end
  
  function main()
      Foo(;a=1, b=2.0) = Foo(a,b)
      bar = Foo(a=1, b=2.0)
      println(bar.a)
  end
  
  main()
  </code></pre>
  
  <p>Why? Is there a workaround?</p>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>Doesn't work from inside a function:</p>
  
  <pre><code>#!/usr/bin/env julia
  
  type Foo
      a::Int64
      b::Int64
  end
  
  function main()
      Foo(;a=1, b=2) = Foo(a,b)
      bar = Foo(a=1, b=2)
      println(bar.a)
  end
  
  main()
  </code></pre>
  
  <p>but if take it out of the function -- it works:</p>
  
  <pre><code>#!/usr/bin/env julia
  
  type Foo
      a::Int64
      b::Int64
  end
  
  # function main()
      Foo(;a=1, b=2) = Foo(a,b)
      bar = Foo(a=1, b=2)
      println(bar.a)
  # end
  
  # main()
  </code></pre>