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

Unintuitive variable name scopes #23000

Closed
Jackels opened this issue Jul 28, 2017 · 3 comments
Closed

Unintuitive variable name scopes #23000

Jackels opened this issue Jul 28, 2017 · 3 comments
Labels
error handling Handling of exceptions by Julia or the user

Comments

@Jackels
Copy link

Jackels commented Jul 28, 2017

let
  function wat()
    a::String = "wat"
    println(a)
  end

  a::String = "me"
  wat()
end

This will throw an exception telling me

ERROR: LoadError: syntax: type of "a" declared in inner scope
while loading /wat.jl, in expression starting on line 1

Which is really not helpful at all. Line 1? Wow, thanks! 'syntax: type of "a" declared in inner scope' that tells me nothing, what is wrong with that. It sounds like there are some words missing e. g. " is not allowed because X".

Now my real issue here is, why is this not legal? Why can I not have a function with a local variables whos name is equal to one from outside the function?

@JeffBezanson
Copy link
Member

Why can I not have a function with a local variables whos name is equal to one from outside the function?

Because the variable is inherited via lexical scope. Putting local on the inner variable will make a new variable.

@JeffBezanson
Copy link
Member

Duplicate of #14959

@JeffBezanson JeffBezanson marked this as a duplicate of #14959 Jul 28, 2017
@JeffBezanson JeffBezanson added the error handling Handling of exceptions by Julia or the user label Aug 24, 2017
@KristofferC
Copy link
Member

Closing as dup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user
Projects
None yet
Development

No branches or pull requests

3 participants