diff --git a/README.md b/README.md index 499b00d0..ddeea12e 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,13 @@ Consequently, **packages should define "private" `load` and `save` methods (also `loadstreaming` and `savestreaming` if you implement them), and not extend (import) FileIO's**. +If you run into a naming conflict with the `load` and `save` functions +(for example, you already have another function in your package that has +one of these names), you can instead name your loaders `fileio_load`, +`fileio_save` etc. Note that you cannot mix and match these styles: either +all your loaders have to be named `load`, or all of them should be called +`fileio_load`, but you cannot use both conventions in one module. + `load(::File)` and `save(::File)` should close any streams they open. (If you use the `do` syntax, this happens for you automatically even if the code inside the `do` scope throws an error.) diff --git a/src/loadsave.jl b/src/loadsave.jl index c10f3a09..f092f556 100755 --- a/src/loadsave.jl +++ b/src/loadsave.jl @@ -173,6 +173,10 @@ for fn in (:load, :loadstreaming, :metadata) for library in libraries try Library = checked_import(library) + gen2_func_name = Symbol("fileio_" * $(string(fn))) + if isdefined(Library, gen2_func_name) + return eval(Library, :($gen2_func_name($q, $args...; $options...))) + end if !has_method_from(methods(Library.$fn), Library) throw(LoaderError(string(library), "$($fn) not defined")) end @@ -193,6 +197,10 @@ for fn in (:save, :savestreaming) for library in libraries try Library = checked_import(library) + gen2_func_name = Symbol("fileio_" * $(string(fn))) + if isdefined(Library, gen2_func_name) + return eval(Library, :($gen2_func_name($q, $data...; $options...))) + end if !has_method_from(methods(Library.$fn), Library) throw(WriterError(string(library), "$($fn) not defined")) end diff --git a/test/loadsave.jl b/test/loadsave.jl index cdfd337b..bb7881af 100644 --- a/test/loadsave.jl +++ b/test/loadsave.jl @@ -7,10 +7,13 @@ module TestLoadSave import FileIO: File, @format_str load(file::File{format"PBMText"}) = "PBMText" load(file::File{format"PBMBinary"}) = "PBMBinary" -load(file::File{format"HDF5"}) = "HDF5" load(file::File{format"JLD"}) = "JLD" load(file::File{format"GZIP"}) = "GZIP" end +module TestLoadSave2 +import FileIO: File, @format_str +fileio_load(file::File{format"HDF5"}) = "HDF5" +end sym2loader = copy(FileIO.sym2loader) sym2saver = copy(FileIO.sym2saver) @@ -23,7 +26,7 @@ try add_loader(format"PBMText", :TestLoadSave) add_loader(format"PBMBinary", :TestLoadSave) - add_loader(format"HDF5", :TestLoadSave) + add_loader(format"HDF5", :TestLoadSave2) add_loader(format"JLD", :TestLoadSave) add_loader(format"GZIP", :TestLoadSave) @@ -285,12 +288,12 @@ load(f::File{format"AmbigExt2"}) = open(f) do io read(stream(io), String) end -save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io +fileio_save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io s = stream(io) print(s, "ambigext1") print(s, testdata) end -save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io +fileio_save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io s = stream(io) print(s, "ambigext2") print(s, testdata)