Picaud Vincent
Picaud Vincent

Reputation: 10982

Status of default initialisation of struct fields?

Under Julia v0.6, the simple code:

julia> struct A
             x::Int = 1
       end   

generates this error:

ERROR: syntax: "x::Int=1" inside type definition is reserved

This is quite an elusive message: reserved for what?

-> Do I have to understand that this kind of definition is going to be allowed in future Julia revisions?

Upvotes: 4

Views: 1856

Answers (1)

Chris Rackauckas
Chris Rackauckas

Reputation: 19132

This is available via Parameters.jl.

julia> using Parameters

julia> @with_kw struct A
           a::Int = 6
           b::Float64 = -1.1
           c::UInt8
       end

julia> A(c=4)
A
  a: 6
  b: -1.1
  c: 4

Upvotes: 7

Related Questions