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

sum = 3 + 7 fails #27

Closed
marshallward opened this issue Oct 22, 2018 · 5 comments
Closed

sum = 3 + 7 fails #27

marshallward opened this issue Oct 22, 2018 · 5 comments

Comments

@marshallward
Copy link

When I try to do this line in the first tutorial (01):

sum = 3 + 7

Then I get this error

ERROR: cannot assign variable Base.sum from module Main

I guess because it's some sort of reserved word in Base.

Does sum need to be renamed?

@xorJane
Copy link
Contributor

xorJane commented Oct 23, 2018

Hey Marshall!

I can't reproduce your error if I run sum = 3 + 7 without any previous use of sum in that notebook; instead 10 prints to stdout as you might expect.

However, Julia does have a built-in sum function and I get the same error you're seeing if I call this function before trying to assign a number to sum. For example, on both JuliaBox and at the REPL I get

ulia> a = [3, 7]
2-element Array{Int64,1}:
 3
 7

julia> sum(a)
10

julia> sum = 3 + 4
ERROR: cannot assign variable Base.sum from module Main

This is mentioned in the Julia docs here:

Julia will even let you redefine built-in constants and functions if needed (although this is not recommended to avoid potential confusions)

However, if you try to redefine a built-in constant or function already in use, Julia will give you an error: [...]

So, really, you should only need to rename your variable sum if you want to make use of the built-in function sum in the same scope. Hope that helps!

@marshallward
Copy link
Author

Yes, this is happening in the REPL. I was not running anything in a workbook so in that case this is probably not relevant.

Feel free to close, if that's the case.

@xorJane
Copy link
Contributor

xorJane commented Oct 23, 2018

The behavior is strange at first, especially if you stumble onto a pre-existing function or constant you weren't aware of, and/or if you're not familiar with Julia's rules for redefining those pre-existing objects. So, your question is very relevant to the audience for the tutorials stored in this repo. I'll close the issue for now, but let us know if you have other questions!

@xorJane xorJane closed this as completed Oct 23, 2018
@mkrasmus
Copy link

mkrasmus commented Dec 5, 2018

Hi I get the same problem.

sum = 3 + 7

Output:
cannot assign variable Base.sum from module Main

Stacktrace:
[1] top-level scope at In[17]:1

@StefanKarpinski
Copy link

Possible culprit: JuliaLang/julia#30234, now fixed. However, if you've previously called the sum function then you will not be able to assign to it by design.

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