Get set in Julia

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Julia

In Julia, get() and set() are used to retrieve and assign values in a dictionary. However, when used in a class, they can be used to retrieve and assign values to class properties.

Here is an example:

# Define a new class
struct Person
    name
    age
end

# Add get and set methods to retrieve and assign values to properties
function Base.getproperty(p::Person, name::Symbol)
if name == :name
return p.name
elseif name == :age
return p.age
else
return nothing
end
end

function Base.setproperty!(p::Person, name::Symbol, value)
if name == :name
p.name = value
elseif name == :age
p.age = value
end
end

# Create a new instance of Person
p = Person("John Doe", 30)

# Get the values of name and age properties using get function
println("Name: ", get(p, :name)) # Output: Name: John Doe
println("Age: ", get(p, :age)) # Output: Age: 30

# Change the value of age property using set function
set(p, :age, 40)

# Get the updated value of age property using get function
println("New Age: ", get(p, :age)) # Output: New Age: 40

In the above example, we have defined a Person class with properties name and age. We added Base.getproperty() and Base.setproperty!() functions to Person class to retrieve and assign values to name and age properties of the class. We also showed how to use get() and set() functions to retrieve and assign class property values.