# Nice way to display functions

**URL:** <https://discourse.julialang.org/t/nice-way-to-display-functions/4089>\
**Category:** General Usage\
**Created:** [June 5, 2017, 9:11am UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089 "2017-06-05T09:11:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [June 5, 2017, 9:11am UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/1 "2017-06-05T09:11:06Z")

</div>

i have a type that takes an anonymous function and want nice output for the display function.  
The anonymous functions should be really short, so showing the source or some kind of compressed syntax tree would be an option.

```julia
type mytype
    f :: Function
end

function show(io::IO, x::mytype)
    println(io, "mytype")
    display(io, x.f)
end

```

update: use `show` instead of `display`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 5, 2017, 12:39pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/2 "2017-06-05T12:39:09Z")

</div>

> [@gdkrmr](#):
>
> `function display(x::mytype)`

Don’t override `display`, override `show`: [https://docs.julialang.org/en/latest/manual/types/#Custom-pretty-printing-1](https://docs.julialang.org/en/latest/manual/types/#Custom-pretty-printing-1)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [June 5, 2017, 2:05pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/3 "2017-06-05T14:05:45Z")

</div>

I would use something like:

```julia
immutable ShortFun{T<:Function}
    f::T
    expr
end
# This printing could be made nicer by removing the `begin` that appears. See MacroTools
Base.show(io::IO, sf::ShortFun) = print(io, sf.expr)
# Beware that kwargs will make this slow; remove them if you don't need them
# and performance is a concern
(sf::ShortFun)(args...; kwargs...) = sf.f(args...; kwargs...)
macro shortfun(expr)
    esc(:(ShortFun($expr, $(Expr(:quote, expr)))))
end
f = @shortfun x -> 2+x

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 5, 2017, 6:36pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/4 "2017-06-05T18:36:02Z")

</div>

> [@gdkrmr](#):
>
> so showing the source or some kind of compressed syntax tree would be an option.

You could output `code_lowered(f)[1]`, i.e. the code for the first method of `f` (likely the only method if `f` is an anonymous function). e.g.

```julia
Base.show(io::IO, x::mytype) = print(io, "mytype: ", code_lowered(x.f)[1])
function Base.show(io::IO, m::MIME"text/plain", x::mytype)
    print(io, "mytype:\n")
    show(io, m, code_lowered(x.f)[1])
end

```

Here, I wrote distinct 2- and 3-argument `show` functions because only in the 3-argument (multiline) case does `code_lowered(x.f)[1]` show the actual lowered code.

(As far as I know, the non-lowered code, i.e. the user’s exact input, is not actually saved unless you do something like the macro trick suggested in another post.)

(Note: `show` should not call `display`, since `display` does not take an `io` parameter.)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 5, 2017, 6:42pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/5 "2017-06-05T18:42:09Z")

</div>

See also the discussion in [https://github.com/JuliaLang/julia/issues/2625](https://github.com/JuliaLang/julia/issues/2625)

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [June 6, 2017, 9:46am UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/6 "2017-06-06T09:46:58Z")

</div>

what about:

```julia
type mytype
    f::Function
    s::String
end

mytype(x::String) = mytype(eval(parse(x)), x)
Base.show(io::IO, x::mytype) = print(io, x.s)

```

feels like `R`

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [June 6, 2017, 7:48pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/7 "2017-06-06T19:48:32Z")

</div>

If it works for you, go for it. The main issue is that it won’t work for closures, but maybe you don’t need that.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [June 6, 2017, 8:03pm UTC](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/8 "2017-06-06T20:03:54Z")

</div>

it’s just for aesthetics, thanks for the input!
