liveBook · Manning

Symbol

As explained in Julia as a Second Language

A symbol is a Julia identifier form that can name a function and, when suitable, enable infix invocation.

A symbol is a Julia identifier form that can name a function. When the name is suitable, the function can also be written between its arguments in infix notation. section 2.5.3

Julia uses several equivalent forms for function calls:

  • Prefix form places the function name before its arguments, as in +(3, 4). section 2.5.3
  • Infix form places a symbolic function name between two arguments, as in 3 + 4. section 2.5.3
  • A regular name such as foo cannot automatically be used in infix form. section 2.5.3

Define an infix operator

Create a function with a symbolic name, then call it either way: ×(a, b) = a^2 + b^2; ×(4, 10) returns 116, and 4 × 10 returns the same result. Type a LaTeX-like abbreviation such as \times and press Tab to enter symbols in the Julia REPL or supported editors. section 2.5.3

Symbols also provide compact programmer-facing text labels. For example, printstyled accepts a color such as :cyan, and Symbol("hello") creates :hello, which compares equal to the literal symbol :hello. section 5.1.1

julia> ×(a, b) = a^2 + b^2
× (generic function with 1 method)
julia> ×(4, 10)
116

julia> 4 × 10
116

julia> 2 × 3
13

Compare prefix calls with infix calls for a user-defined symbolic function. section 2.5.3

How Julia as a Second Language covers it

How deep it goes
Defines symbols through runnable calls and a custom operator, then demonstrates symbol construction; it does not develop deeper implementation details.
Examples
Demonstrates +, -, a custom × function, printstyled color :cyan, and Symbol("hello") compared with :hello.
What it assumes
Assumes basic Julia function calls and strings while teaching prefix, infix, suffix, symbol literals, and Symbol construction.
What you can do after
Apply it via Julia syntax to call symbolic functions in prefix or infix form and supply symbolic labels to printstyled.
How it connects
Connects symbols with functions, infix notation, Unicode input, strings, keyword arguments, and the Julia REPL.

Also explained in

Compare how 5 books explain symbol →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.