Implementing read access and lazy loading

In order to implement lazy loading, we can extend the getproperty function. During the call, we can check whether the file content has been loaded yet. If not, we just load the file content right before returning the data back to the caller.

Extending the getproperty function is as easy as simply defining it with the FileContent type and a symbol as the arguments of the function. The following code shows this:

function Base.getproperty(fc::FileContent, s::Symbol)    direct_passthrough_fields = (:path, )    if s in direct_passthrough_fields        return getfield(fc, s)    end    if s === :contents        !getfield(fc, :loaded) && load_contents!(fc)        return getfield(fc, :contents)    end    error("Unsupported property: $s")end

It ...

Get Hands-On Design Patterns and Best Practices with Julia now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.