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 append to an array in JuliaLang?
- How to install JuliaLang?
- How to sort in JuliaLang?
- How to create plots in JuliaLang?
- How to solve differential equations in JuliaLang?
- How to use channels in JuliaLang?
- How to use try catch in JuliaLang?
- How to work with rational numbers in JuliaLang?
- How to work with matrices in JuliaLang?
See more codes...