1
votes

I am developing a Provider package in Julia which would be used by multiple Consumer packages. Below is a minimal example of the Project structure for the Provider package and a sample Consumer package called Consumer_A to reproduce the issue :

enter image description here

The file generic.jl in the Provider package defines an abstract type AbstractDataLoader with a function load_data. This function is overridden in the concrete type CustomDataLoader defined in the file custom_implementation.jl in the Consumer_A package as shown below:

generic.jl :

export DataProcessor, process_data

abstract type AbstractDataLoader end
function load_data(data_loader::AbstractDataLoader)
    error("No load_data function defined for $(typeof(data_loader))")
end

struct DataProcessor end
function process_data(data_loader::AbstractDataLoader)
    data = load_data(data_loader)
    println("do some processing after loading data: $data")
end

custom_implementation.jl :

import Provider

export CustomDataLoader, load_data

struct CustomDataLoader <: Provider.AbstractDataLoader  end
function load_data(data_loader::CustomDataLoader)
    return "sample data"
end

The main.jl file has a main function which instantiates the concrete type and calls a method process_data that in turn should ideally call the overriding function:

using Provider

export main

function main()
    data_loader = CustomDataLoader()
    data_processor = DataProcessor()
    process_data(data_loader)
end

But instead, on running main() I get the error "No load_data function defined for CustomDataLoader" that is raised in the generic.jl load_data function. How do I ensure that the overriding concrete type function gets called in this case?

1

1 Answers

1
votes

In custom_implementation.jl , I tried replacing function load_data with function Provider.load_data because I was using the statement import Provider instead of using Provider as I wanted to override the definition. With this change, it works fine and the overriding function gets called!

The updated custom_implementation.jl is now:

import Provider

export CustomDataLoader, load_data

struct CustomDataLoader <: Provider.AbstractDataLoader  end
function Provider.load_data(data_loader::CustomDataLoader)
    return "sample data"
end