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 ...