Commit 04bfa32
committed
Make functions print as valid julia expressions.
Previously, function instances would be printed in `static_show()` as
elements of a `DataType`, with no special printing for `<:Function`s.
This led to printing them via _invalid_ syntax, since there is no empty
constructor for `T<:Function`.
Before:
```julia
julia> g() = 2
g (generic function with 1 method)
julia> println(static_shown(g))
typeof(Main.g)()
julia> eval(Meta.parse(static_shown(g)))
ERROR: MethodError: no method matching typeof(g)()
```
After:
```julia
julia> g() = 2
g (generic function with 1 method)
julia> println(static_shown(g))
Main.g
julia> eval(Meta.parse(static_shown(g)))
g (generic function with 1 method)
```
I chose to keep the Module prefix on the functions, so that they will
be valid julia expressions that you can enter at the REPL to reproduce
the function instance, even though this is a break from the julia
`show()` behavior.
One caveat is that this is still not correct for anonymous functions,
but I'm also not really sure what would be a better way to print
anonymous functions... I think this is _probably_ better than it was
before, but very open to suggestions for how to improve it.
Before:
```julia
julia> l = (x,y)->x+y
julia> println(static_shown(l))
getfield(Main, Symbol("#3#4"))()
julia> eval(Meta.parse(static_shown(l)))
ERROR: MethodError: no method matching var"#3#4"()
```
After:
```julia
julia> l = (x,y)->x+y
julia> println(static_shown(l))
Main.var"#3"
julia> eval(Meta.parse(static_shown(l)))
ERROR: UndefVarError: #3 not defined
```1 parent 3599557 commit 04bfa32
3 files changed
+55
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1105 | 1105 | | |
1106 | 1106 | | |
1107 | 1107 | | |
| 1108 | + | |
1108 | 1109 | | |
1109 | 1110 | | |
1110 | 1111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
604 | 604 | | |
605 | 605 | | |
606 | 606 | | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
607 | 623 | | |
608 | 624 | | |
609 | 625 | | |
| |||
688 | 704 | | |
689 | 705 | | |
690 | 706 | | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
| 707 | + | |
| 708 | + | |
700 | 709 | | |
701 | 710 | | |
702 | | - | |
703 | 711 | | |
704 | 712 | | |
705 | 713 | | |
706 | 714 | | |
707 | 715 | | |
708 | 716 | | |
709 | 717 | | |
| 718 | + | |
710 | 719 | | |
711 | 720 | | |
712 | 721 | | |
| |||
715 | 724 | | |
716 | 725 | | |
717 | 726 | | |
718 | | - | |
| 727 | + | |
719 | 728 | | |
| 729 | + | |
720 | 730 | | |
721 | 731 | | |
722 | 732 | | |
| |||
982 | 992 | | |
983 | 993 | | |
984 | 994 | | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
985 | 1022 | | |
986 | 1023 | | |
987 | 1024 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1338 | 1338 | | |
1339 | 1339 | | |
1340 | 1340 | | |
| 1341 | + | |
1341 | 1342 | | |
1342 | 1343 | | |
1343 | 1344 | | |
| |||
1349 | 1350 | | |
1350 | 1351 | | |
1351 | 1352 | | |
| 1353 | + | |
1352 | 1354 | | |
1353 | 1355 | | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
1354 | 1360 | | |
1355 | 1361 | | |
1356 | 1362 | | |
| |||
0 commit comments