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

Should method extension return the extended function? #26258

Open
fredrikekre opened this issue Feb 28, 2018 · 2 comments
Open

Should method extension return the extended function? #26258

fredrikekre opened this issue Feb 28, 2018 · 2 comments
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@fredrikekre
Copy link
Member

fredrikekre commented Feb 28, 2018

Consider the difference between these two function definitions

julia> Base.sum() = 1

julia> foo() = 1
foo (generic function with 1 method)

Should the method extension here return the sum function? I think it would be nice if the example above instead behaved like:

julia> Base.sum() = 1
sum (generic function with 16 methods)

Amusingly this is actually causing a doctest failure.

@StefanKarpinski StefanKarpinski added the kind:bug Indicates an unexpected problem or unintended behavior label Feb 28, 2018
@Moelf
Copy link
Sponsor Contributor

Moelf commented Oct 30, 2023

I think the displaying is:

julia/base/show.jl

Lines 34 to 41 in 96147bb

function show(io::IO, ::MIME"text/plain", f::Function)
get(io, :compact, false)::Bool && return show(io, f)
ft = typeof(f)
name = ft.name.mt.name
if isa(f, Core.IntrinsicFunction)
print(io, f)
id = Core.Intrinsics.bitcast(Int32, f)
print(io, " (intrinsic function #$id)")

but I can't figure out what controls what to return when we have a statement like

julia> Base.sum() = 1

@vtjnash
Copy link
Sponsor Member

vtjnash commented Oct 30, 2023

It is part of lowering (see return *):

julia> Meta.@lower Base.x() = 1
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1%1 = Base.getproperty(Base, :x)
│   %2 = Core.Typeof(%1)
│   %3 = Core.svec(%2)
│   %4 = Core.svec()
│   %5 = Core.svec(%3, %4, $(QuoteNode(:(#= REPL[1]:1 =#))))
│        $(Expr(:method, nothing, :(%5), CodeInfo(
    @ REPL[1]:1 within `none`
1return 1
)))
└──      return nothing
))))

julia> Meta.@lower x() = 1
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1$(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1return $(Expr(:method, :x))
)))
│        $(Expr(:method, :x))
│   %3 = Core.Typeof(x)
│   %4 = Core.svec(%3)
│   %5 = Core.svec()
│   %6 = Core.svec(%4, %5, $(QuoteNode(:(#= REPL[2]:1 =#))))
│        $(Expr(:method, :x, :(%6), CodeInfo(
    @ REPL[2]:1 within `none`
1return 1
)))
└──      return x
))))

@vtjnash vtjnash added compiler:lowering Syntax lowering (compiler front end, 2nd stage) and removed kind:bug Indicates an unexpected problem or unintended behavior labels Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

4 participants