Skip to content

How to import a local module into another in different separate files(with out breaking VS code tooling) #777

@abannsunny

Description

@abannsunny

I wanted to know how to import a local module in a file into another module in a different file.

so for instance, I have a situation like this:

#inside main.jl
include("M2.jl")
include("M1.jl")


function DoSomething(inputStringbody)
parsedJson = JSON.parse(inputStringbody)

ofTypeDataType = M2.makeDataType(parsedJson)

M1.useDataTypeToProcess(ofTypeDataType )

end

end
#inside file M1.jl
module M1
function useDataTypeToProcess( dataTypeParameter:: DataTypeModule.DataType)
# do some processing with DataType
end

end
#inside file M2.jl
module M2

include("DataTypeModule.jl")
using Main.DataTypeModule

function makeDataType(someJson)

DataTypeModule.DataType(someJson["name"], someJson["id"])

end 
end
#inside file DataTypeModule.jl
module DataTypeModule

struct DataType

    name::String

     id::Int64

    function DataType(

        name::String,

        id::Int64

        )

new(name,
id)


    end


end

If I run DoSomething(data), it breaks with a type mismatches saying:

MethodError: no method matching useDataTypeToProcess(::Main.M2.DataTypeModule.DataType)

Closest candidates are:
seDataTypeToProcess(!Matched::Main.DataTypeModule.DataType)

I could get around it by removing the include statement
include(“DataTypeModule.jl”)
and just use the using statement
using Main.DataTypeModule

But that breaks the tooling on the visual studio code. (Julia language support). Since it does not know where the DataTypeModule module is imported from.

So I was wondering how do you import a module so that it maintains the types and not break the useful tooling in the IDE.

Here is a stack overflow thread on importing modules if it helps:
https://stackoverflow.com/a/48774908/5522453

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions