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

function definitions inside try / catch block behave differently in 0.5 - intended? #18767

Closed
tauu opened this issue Oct 2, 2016 · 6 comments

Comments

@tauu
Copy link

tauu commented Oct 2, 2016

The following code behaves differently in julia v0.4 and julia v0.5

try
  function test(x)
    return x+1;
  end
end

In Julia v0.4 it returns

  test (generic function with 1 method)

whereas in Julia v0.5 I get

  (::test) (generic function with 1 method)

So in Julia v0.5 the method is not defined in global scope. This is even the case if a function in global scope exists. For Example:

import Base.show
type myType
  text::AbstractString
end

try
  function show(io::IO,::MIME"text/plain",mt::myType)
    return string("-- ", mt.text, " --");
  end
end

will actually not create a new method for the function Base.show, but a new local function show with one method.

Is this change intended? I couldn't find anything about it in the release notes.

@JeffBezanson
Copy link
Member

It looks like this was a bug in 0.4, since try blocks introduce scopes. For example in 0.4:

julia> for i=1:1
        function test(x)
          x+1
        end
       end

julia> test
ERROR: UndefVarError: test not defined

so other blocks that introduce scopes behaved this way before, but not try blocks for some reason.

@TotalVerb
Copy link
Contributor

Use global function show(...) instead of function show(...) to explicitly define a function in global scope.

tauu pushed a commit to tauu/Gadfly.jl that referenced this issue Oct 4, 2016
Try blocks behave differently in julia v0.5 compared to v0.4. The new
behaviour caused the show methods requiring Cairo to be defined in
local scope instead of global scope, which in turn prevented them from
being used. See also:
  JuliaLang/julia#18767
@tauu
Copy link
Author

tauu commented Oct 4, 2016

Thanks for the clarification! :-)

try global function show(...) end works in v0.5 👍 , but doesn't work in v0.4 :-/ So its not ideal for backwards compatibility, but at least it works :-)

@TotalVerb
Copy link
Contributor

This should work on both:

try
    global foo
    function foo(...) ... end

@tauu
Copy link
Author

tauu commented Oct 4, 2016

works, thanks! :-)

@kshyatt
Copy link
Contributor

kshyatt commented Oct 4, 2016

works, thanks! :-)

Sounds like this was resolved? Feel free to reopen if not, @tauu :).

@kshyatt kshyatt closed this as completed Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants