lanselibai
lanselibai

Reputation: 1273

Name a variable with an exclamation in Julia

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.

Upvotes: 2

Views: 263

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69839

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.

Upvotes: 5

Related Questions