Application Code Organization

That is one alternative. Another is to let the structure types “invisible” to the user. For example:

This would be implemented in the package:

julia> struct WithFile
         f :: String
       end

julia> struct WithoutFile
       end

julia> preprocess_data() = return WithoutFile()
preprocess_data (generic function with 1 method)

julia> preprocess_data(f::String) = return WithFile(f)
preprocess_data (generic function with 2 methods)

julia> compute(data::WithoutFile) = "Computation without file"
compute (generic function with 1 method)

julia> compute(data::WithFile) = "Computation with file: $(data.f)"
compute (generic function with 2 methods)

The “user” starts here:

julia> user_data_without_file = preprocess_data()
WithoutFile()

julia> compute(user_data_without_file)
"Computation without file"

julia> user_data_with_file = preprocess_data("file.data")
WithFile("file.data")

julia> compute(user_data_with_file)
"Computation with file: file.data"


Actually there are, the functors.

2 Likes