From 6f0682f45639bfe0c69e8f12b6b959e2a8eb82ea Mon Sep 17 00:00:00 2001 From: Markus Kuhn Date: Sun, 13 Jan 2019 13:17:17 +0000 Subject: [PATCH] enable sprint() to pass on kwargs A new optional keyword argument `kwargs` can now take a named tuple and Base.sprint will then pass on the content of that tuple as keyword arguments to the function called. --- base/strings/io.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/base/strings/io.jl b/base/strings/io.jl index 7453845211148..d7e73f93c5d02 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -71,7 +71,7 @@ println(io::IO, xs...) = print(io, xs..., '\n') ## conversion of general objects to strings ## """ - sprint(f::Function, args...; context=nothing, sizehint=0) + sprint(f::Function, args...; context=nothing, sizehint=0, kwargs=NamedTuple()) Call the given function with an I/O stream and the supplied extra arguments. Everything written to this I/O stream is returned as a string. @@ -82,7 +82,8 @@ of the buffer (in bytes). The optional keyword argument `context` can be set to `:key=>value` pair or an `IO` or [`IOContext`](@ref) object whose attributes are used for the I/O stream passed to `f`. The optional `sizehint` is a suggested size (in bytes) -to allocate for the buffer used to write the string. +to allocate for the buffer used to write the string. The optional +`kwargs` takes a named tuple and passes it on to `f` as keyword arguments. # Examples ```jldoctest @@ -93,12 +94,13 @@ julia> sprint(showerror, BoundsError([1], 100)) "BoundsError: attempt to access 1-element Array{Int64,1} at index [100]" ``` """ -function sprint(f::Function, args...; context=nothing, sizehint::Integer=0) +function sprint(f::Function, args...; context=nothing, sizehint::Integer=0, + kwargs::NamedTuple=NamedTuple()) s = IOBuffer(sizehint=sizehint) if context !== nothing - f(IOContext(s, context), args...) + f(IOContext(s, context), args...; kwargs...) else - f(s, args...) + f(s, args...; kwargs...) end String(resize!(s.data, s.size)) end