Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable sprint() to pass on kwargs #30708

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down