Skip to content
Open
Show file tree
Hide file tree
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
89 changes: 65 additions & 24 deletions src/MappedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,38 +226,79 @@ function Base.showarg(io::IO, A::AbstractMappedArray{T,N}, toplevel=false) where
toplevel && print(io, " with eltype ", T)
end

function func_print(io, f, types)
ft = typeof(f)
mt = ft.name.mt
name = string(mt.name)
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
@static if VERSION >= v"1.12.0"
# for Julia 1.12 and later

function func_print(io, f, types)
ft = typeof(f)
name = string(Base.nameof(ft))
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
show(io, f)
return nothing
end
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || isa(c[2], Core.ReturnNode))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
end
print(io, c1)
else
show(io, f)
end
else
show(io, f)
return nothing
end
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || (isdefined(Core, :ReturnNode) && isa(c[2], Core.ReturnNode)))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end

else
# for Julia pre-1.12

function func_print(io, f, types)
ft = typeof(f)
mt = ft.name.mt
name = string(mt.name)
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
show(io, f)
return nothing
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || (isdefined(Core, :ReturnNode) && isa(c[2], Core.ReturnNode)))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
end
print(io, c1)
else
show(io, f)
end
print(io, c1)
else
show(io, f)
end
else
show(io, f)
end

end

eltypes(A::AbstractArray) = Tuple{eltype(A)}
Expand Down
12 changes: 10 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,21 @@ end
b = mappedarray(sqrt, a)
@test summary(b) == "4-element mappedarray(sqrt, ::$(Vector{Int})) with eltype Float64"
c = mappedarray(sqrt, x->x*x, a)
@test summary(c) == "4-element mappedarray(sqrt, x->x * x, ::$(Vector{Int})) with eltype Float64"
if VERSION >= v"1.12.0"
@test summary(c) == "4-element mappedarray(sqrt, var\"#21#22\"(), ::$(Vector{Int})) with eltype Float64"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var\"#21#22\" seems like it could be fragile in a test; what if we modify the package so it creates additional gensyms?

Likewise below

else
@test summary(c) == "4-element mappedarray(sqrt, x->x * x, ::$(Vector{Int})) with eltype Float64"
end
# issue #26
M = @inferred mappedarray((x1,x2)->x1+x2, a, a)
io = IOBuffer()
show(io, MIME("text/plain"), M)
str = String(take!(io))
@test occursin("x1 + x2", str)
if VERSION >= v"1.12.0"
@test occursin("var\"#23#24\"()", str)
else
@test occursin("x1 + x2", str)
end
end

@testset "eltype (issue #32)" begin
Expand Down
Loading