From 286f803752c70e655924e4b55925e982b52ae83f Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Thu, 20 Jan 2022 02:27:34 +0000 Subject: [PATCH] Test/docs/news --- NEWS.md | 1 + base/strings/lazy.jl | 13 +++++++++++++ test/strings/basic.jl | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/NEWS.md b/NEWS.md index 3f23aaf573b72..f5979535d1186 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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]) diff --git a/base/strings/lazy.jl b/base/strings/lazy.jl index 8dc8ed3e91031..fc046e34f6fd1 100644 --- a/base/strings/lazy.jl +++ b/base/strings/lazy.jl @@ -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 @@ -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 diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 2bdac1b353e1f..1da897667a2ea 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -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 \ No newline at end of file