I would like to create a composite type, initialize some constant objects of that type, and then use the objects with methods I’ve coded. For example, below I have created a folder with 3 files:
-
RectangleMod.jl
- Defines theRectangle
type and a corresponding method to calculate its area:
module RectangleMod
export Rectangle, get_area
struct Rectangle
width::Number
height::Number
end
function get_area(rectangle::Rectangle)
return rectangle.width * rectangle.height;
end
end
-
savedRectangles.jl
- Stores an object ofRectangle
type
module savedRectangles
include("./RectangleMod.jl")
using .RectangleMod
export MY_RECTANGLE
const MY_RECTANGLE = Rectangle(1.0, 2.0);
end
-
test_rectangle.jl
- ImportsMY_RECTANGLE
and tries to calculate its area:
include("./savedRectangles.jl")
include("./RectangleMod.jl")
using .savedRectangles
using .RectangleMod
get_area(MY_RECTANGLE)
However, I get the following error when I run test_rectangle.jl
:
ERROR: LoadError: MethodError: no method matching get_area(::Main.savedRectangles.RectangleMod.Rectangle)
Closest candidates are:
get_area(::Rectangle) at c:\Users\jlym\Downloads\test_rectangle\RectangleMod.jl:10
Stacktrace:
[1] top-level scope
@ c:\Users\jlym\Downloads\test_rectangle\test_rectangle.jl:7
in expression starting at c:\Users\jlym\Downloads\test_rectangle\test_rectangle.jl:7
The stacktrace suggests MY_RECTANGLE
is type ::Main.savedRectangles.RectangleMod.Rectangle
, which is not recognized as a ::Rectangle
type. What can I do to resolve this?
EDIT: Updated code snippers to enable syntax highlighting