A Poor
A Poor

Reputation: 1054

How can I set default parameters for mutable structs in Julia?

Is there a way to add default parameters for mutable structs in Julia?

I'm trying to write something like the following:

mutable struct Scale
    # Set default values that will be changed by fit!()
    domain_min::Float64 = 0.0
    domain_max::Float64 = 1.0
    range_min::Float64  = 0.0 
    range_max::Float64  = 1.0
end

function fit!(data::Array)
    # Set struct params here using `data`
end

Is there a way to do this or should I try a different approach?

Upvotes: 10

Views: 4423

Answers (3)

Oskar
Oskar

Reputation: 1460

Or you can also just go the long way and define it yourself with constructors, as you would normally do if you want to instantiate it in several possible ways.

mutable struct Scale                                                                                                                                                                                                                          
    # Set default values that will be changed by fit!()
    domain_min::Float64      
    domain_max::Float64      
    range_min::Float64        
    range_max::Float64       
end

# With default values, but no keywords
julia> Scale(dmin=1.,dmax=2.,rmin=1.,rmax=2.) = Scale(dmin, dmax, rmin, rmax)
Scale

julia> Scale(3.,4.)
Scale(3.0, 4.0, 1.0, 2.0)

# With keyword arguments:
julia> Scale(;dmin=1.,dmax=2.,rmin=1.,rmax=2.) = Scale(dmin, dmax, rmin, rmax)
Scale

julia> Scale(rmax=3., rmin=1.2)
Scale(1.0, 2.0, 1.2, 3.0)

Notice the difference between the two constructors, one has a semicolon ; the other not. I would not recommend using both constructors at the same time, this may lead to some confusion.

Upvotes: 6

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

I prefer using Parameters.jl because it provides also a nicer way the structs are displayed which is much nicer for debugging:

julia> using Parameters

julia> @with_kw struct A          
       a::Int=5                   
       b::String="hello"          
       c::Float64                 
       end;                                 
                                  
julia> A(c=3.5)                   
A                                 
  a: Int64 5                      
  b: String "hello"               
  c: Float64 3.5                  

Upvotes: 8

This is exactly what Base.@kwdef does:

julia> Base.@kwdef mutable struct Scale
           # Set default values that will be changed by fit!()
           domain_min::Float64 = 0.0
           domain_max::Float64 = 1.0
           range_min::Float64  = 0.0 
           range_max::Float64  = 1.0
       end
Scale

# All parameters to their default values
julia> Scale()
Scale(0.0, 1.0, 0.0, 1.0)

# Specify some parameter(s) using keyword argument(s)
julia> Scale(range_min = 0.5)
Scale(0.0, 1.0, 0.5, 1.0)

Upvotes: 10

Related Questions