Multiple fields per line in struct declaration

I have a struct with many fields, which can be logically grouped, like this, but more fields:

mutable struct Limits
  vMin::Float32
  vMax::Float32
  aMin::Float32
  aMax::Float32
end

Is there any way to write it similar to:

mutable struct Limits
  vMin::Float32, vMax::Float32
  aMin::Float32, aMax::Float32
end

or perhaps closer to (don’t mind the syntax):

mutable struct Limits
  vMin, vMax, aMin, aMax ::Float32
end

with the type specified only once, but applicable to all fields in that line?

You can do:

mutable struct Limits
    vMin::Float32; vMax::Float32
    aMin::Float32; aMax::Float32
end

As newlines and semicolons are interchangeable.

9 Likes

Or you can do a

mutable struct Limits{T}
  vMin::T
  vMax::T
  aMin::T
  aMax::T
end

and just use the type for Float32. That said, whenever I work with

I try to organize them somewhat into smaller structures or tuples. Eg if you don’t need to modify extrema separately, you can do

mutable struct Limits{T}
    v_extrema::Tuple{T,T}
    a_extrema::Tuple{T,T}
end

or even define a (mutable) struct for extrema, depending on your application.

5 Likes

With QuickTypes

julia> using QuickTypes

julia> @qmutable Limits(vMin::Float32, vMax::Float32,
                        aMin::Float32, aMax::Float32)
3 Likes