Skip to content

Commit

Permalink
Test/docs/news
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Jan 20, 2022
1 parent 4d68d93 commit 286f803
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Standard library changes
* TCP socket objects now expose `closewrite` functionality and support half-open mode usage ([#40783]).
* Intersect returns a result with the eltype of the type-promoted eltypes of the two inputs ([#41769]).
* `Iterators.countfrom` now accepts any type that defines `+`. ([#37747])
* The `LazyString` and the `lazy"str"` macro were added to support delayed construction of error messages in error paths. ([#33711])

#### InteractiveUtils
* A new macro `@time_imports` for reporting any time spent importing packages and their dependencies ([#41612])
Expand Down
13 changes: 13 additions & 0 deletions base/strings/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ of functions).
This type is designed to be cheap to construct at runtime, trying to offload
as much work as possible to either the macro or later printing operations.
!!! compat "Julia 1.8"
`LazyString` requires Julia 1.8 or later.
"""
mutable struct LazyString <: AbstractString
parts::Tuple
Expand All @@ -16,6 +19,16 @@ mutable struct LazyString <: AbstractString
LazyString(args...) = new(args)
end

"""
lazy"str"
Create a [`LazyString`](@ref) using regular string interpolation syntax.
Note that interpolations are *evaluated* at LazyString construction time,
but *printing* is delayed until the first access to the string.
!!! compat "Julia 1.8"
`lazy"str"` requires Julia 1.8 or later.
"""
macro lazy_str(text)
parts = Any[]
lastidx = idx = 1
Expand Down
7 changes: 7 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1094,3 +1094,10 @@ end
@test sprint(summary, SubString("foα", 2)) == "3-codeunit SubString{String}"
@test sprint(summary, "") == "empty String"
end

@testset "LazyString" begin
@test repr(lazy"$(1+2) is 3") == "\"3 is 3\""
let d = Dict(lazy"$(1+2) is 3" => 3)
@test d["3 is 3"] == 3
end
end

0 comments on commit 286f803

Please sign in to comment.