Skip to content

Commit 0f5930c

Browse files
committed
brings duplication down to previous levels
1 parent 5fb37db commit 0f5930c

File tree

1 file changed

+32
-68
lines changed

1 file changed

+32
-68
lines changed

src/loadsave.jl

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -142,83 +142,47 @@ end
142142

143143
# Handlers for formatted files/streams
144144

145-
# TODO: this definitely should be refactored to reduce duplication
146-
function load{F}(q::Formatted{F}, args...; options...)
147-
if unknown(q)
148-
isfile(filename(q)) || open(filename(q)) # force systemerror
149-
throw(UnknownFormat(q))
150-
end
151-
libraries = applicable_loaders(q)
152-
failures = Any[]
153-
for library in libraries
154-
try
155-
Library = checked_import(library)
156-
if !has_method_from(methods(Library.load), Library)
157-
throw(LoaderError(string(library), "load not defined"))
158-
end
159-
return eval(Main, :($(Library.load)($q, $args...; $options...)))
160-
catch e
161-
push!(failures, (e, q))
145+
for fn in (:load, :loadstreaming)
146+
@eval function $fn{F}(q::Formatted{F}, args...; options...)
147+
if unknown(q)
148+
isfile(filename(q)) || open(filename(q)) # force systemerror
149+
throw(UnknownFormat(q))
162150
end
163-
end
164-
handle_exceptions(failures, "loading \"$(filename(q))\"")
165-
end
166-
167-
function loadstreaming{F}(q::Formatted{F}, args...; options...)
168-
if unknown(q)
169-
isfile(filename(q)) || open(filename(q)) # force systemerror
170-
throw(UnknownFormat(q))
171-
end
172-
libraries = applicable_loaders(q)
173-
failures = Any[]
174-
for library in libraries
175-
try
176-
Library = checked_import(library)
177-
if !has_method_from(methods(Library.loadstreaming), Library)
178-
throw(LoaderError(string(library), "loadstreaming not defined"))
151+
libraries = applicable_loaders(q)
152+
failures = Any[]
153+
for library in libraries
154+
try
155+
Library = checked_import(library)
156+
if !has_method_from(methods(Library.$fn), Library)
157+
throw(LoaderError(string(library), "$($fn) not defined"))
158+
end
159+
return eval(Main, :($(Library.$fn)($q, $args...; $options...)))
160+
catch e
161+
push!(failures, (e, q))
179162
end
180-
return eval(Main, :($(Library.loadstreaming)($q, $args...; $options...)))
181-
catch e
182-
push!(failures, (e, q))
183163
end
164+
handle_exceptions(failures, "loading \"$(filename(q))\"")
184165
end
185-
handle_exceptions(failures, "opening \"$(filename(q))\" for streamed loading")
186166
end
187167

188-
function save{F}(q::Formatted{F}, data...; options...)
189-
unknown(q) && throw(UnknownFormat(q))
190-
libraries = applicable_savers(q)
191-
failures = Any[]
192-
for library in libraries
193-
try
194-
Library = checked_import(library)
195-
if !has_method_from(methods(Library.save), Library)
196-
throw(WriterError(string(library), "save not defined"))
197-
end
198-
return eval(Main, :($(Library.save)($q, $data...; $options...)))
199-
catch e
200-
push!(failures, (e, q))
201-
end
202-
end
203-
handle_exceptions(failures, "saving \"$(filename(q))\"")
204-
end
205-
206-
function savestreaming{F}(q::Formatted{F}, data...; options...)
207-
unknown(q) && throw(UnknownFormat(q))
208-
libraries = applicable_savers(q)
209-
failures = Any[]
210-
for library in libraries
211-
try
212-
Library = checked_import(library)
213-
if !has_method_from(methods(Library.savestreaming), Library)
214-
throw(WriterError(string(library), "savestreaming not defined"))
168+
for fn in (:save, :savestreaming)
169+
@eval function $fn{F}(q::Formatted{F}, data...; options...)
170+
unknown(q) && throw(UnknownFormat(q))
171+
libraries = applicable_savers(q)
172+
failures = Any[]
173+
for library in libraries
174+
try
175+
Library = checked_import(library)
176+
if !has_method_from(methods(Library.$fn), Library)
177+
throw(WriterError(string(library), "$($fn) not defined"))
178+
end
179+
return eval(Main, :($(Library.$fn)($q, $data...; $options...)))
180+
catch e
181+
push!(failures, (e, q))
215182
end
216-
return eval(Main, :($(Library.savestreaming)($q, $data...; $options...)))
217-
catch e
218-
push!(failures, (e, q))
219183
end
184+
handle_exceptions(failures, "saving \"$(filename(q))\"")
220185
end
221-
handle_exceptions(failures, "opening \"$(filename(q))\" for streamed saving")
222186
end
223187

224188
# returns true if the given method table includes a method defined by the given

0 commit comments

Comments
 (0)