Skip to content

Commit a41c8b9

Browse files
authored
Merge pull request #51 from AayushSabharwal/as/lazy-str
refactor: avoid building strings in `throw=false` path of `splitdef`
2 parents 94b06a5 + 6a4b37d commit a41c8b9

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/ExprTools.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module ExprTools
22

33
export args_tuple_expr, combinedef, parameters, signature, splitdef
44

5+
include("compat.jl")
56
include("function.jl")
67
include("method.jl")
78
include("type_utils.jl")
89
include("def_tools.jl")
910

10-
end # module
11+
end # module

src/compat.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@static if !isdefined(Base, :LazyString)
2+
# `LazyString` was added in Julia 1.8. On older versions fall back to eagerly
3+
# building the string; correctness is preserved, only the laziness is lost.
4+
LazyString(parts...) = string(parts...)
5+
end
6+

src/function.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ function splitdef(ex::Expr; throw::Bool=true)
2626

2727
function invalid_def(section)
2828
if throw
29-
msg = "Function definition contains $section\n$(sprint(Meta.dump, full_ex))"
29+
msg = LazyString(
30+
"Function definition contains ", section, "\n",
31+
sprint(Meta.dump, full_ex),
32+
)
3033
Base.throw(ArgumentError(msg))
3134
else
3235
nothing
3336
end
3437
end
3538

3639
if !(ex.head === :function || ex.head === :(=) || ex.head === :(->))
37-
return invalid_def("invalid function head `$(repr(ex.head))`")
40+
return invalid_def(LazyString("invalid function head `", ex.head, "`"))
3841
end
3942

4043
def[:head] = ex.head
@@ -48,7 +51,9 @@ function splitdef(ex::Expr; throw::Bool=true)
4851
ex = ex.args[1] # Focus on the function signature
4952
else
5053
quan = length(ex.args) > 2 ? "too many" : "too few"
51-
return invalid_def("$quan of expression arguments for `$(repr(def[:head]))`")
54+
return invalid_def(
55+
LazyString(quan, " of expression arguments for `", def[:head], "`"),
56+
)
5257
end
5358

5459
# Where parameters

0 commit comments

Comments
 (0)