2

I got confused about using an exclamation to name a variable in this link.

Firstly, it works fine in the JuliaPro Command Prompt

enter image description here

Then, I closed the JuliaPro Command Prompt and opened it again, trying to test different variable namings:

enter image description here

I could not understand how to use an exclamation.

lanselibai
  • 1,203
  • 2
  • 19
  • 35
  • 1
    Always put spaces between operators and variables or literals. It is good practice, and makes your code prettier and easier to read. And you avoid parsing pitfalls such as this. – DNF Feb 11 '18 at 17:40

1 Answers1

5

Add a space after !. Without a space Julia treats != as inequality test.

You can check how Julia parses an expression by using parse function and sending the required expression in a string (and then using dump to see the parsed structure), e.g.:

julia> parse("x! =1")
:(x! = 1)

julia> dump(parse("x! =1"))
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Symbol x!
    2: Int64 1
  typ: Any

julia> parse("x!=1")
:(x != 1)

julia> dump(parse("x!=1"))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol !=
    2: Symbol x
    3: Int64 1
  typ: Any

And you can see that the first expression is an assignment and the second is a call to != function.

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107