julialangHow to use constructors in JuliaLang?
Constructors are used to create objects in JuliaLang. They are functions that take arguments and return an object.
Example
struct Point
x::Float64
y::Float64
end
function Point(x, y)
Point(x, y)
end
p = Point(1.0, 2.0)
Output example
Point(1.0, 2.0)
Code explanation
struct Point: defines a new type calledPointx::Float64andy::Float64: define two fields of typeFloat64function Point(x, y): defines a constructor function for thePointtypePoint(x, y): creates a newPointobject with the givenxandyvaluesp = Point(1.0, 2.0): creates a newPointobject withxandyvalues of1.0and2.0respectively
Helpful links
More of Julialang
- How to test code in JuliaLang?
- How to get JuliaLang version?
- How to work with CSV in JuliaLang?
- How to append to an array in JuliaLang?
- How to create a histogram in JuliaLang?
- How to sort in JuliaLang?
- How to use regular expressions in JuliaLang?
- How to calculate the mean in JuliaLang?
- How to add a legend to a plot in JuliaLang?
- How to use the JuliaLang PackageCompiler?
See more codes...