# Is it possible to use x² to mean taking square x^2

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137>\
**Category:** General Usage\
**Created:** [February 10, 2022, 1:50am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137 "2022-02-10T01:50:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 10, 2022, 1:50am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/1 "2022-02-10T01:50:25Z")

</div>

Say, x^2 can be written as x², tanspose(M) can be written as Mᵀ, and so on?

---

<div class="post-metadata">

**Author:** ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)\
**Post date:** [February 10, 2022, 1:59am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/2 "2022-02-10T01:59:17Z")

</div>

I like it

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 10, 2022, 2:26am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/3 "2022-02-10T02:26:36Z")

</div>

no, because Julia allows unicode variable names:

```julia
julia> x² = 3
3
- - -

julia> Meta.@lower x²
:x²

```

it’s just one token

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 10, 2022, 2:34am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/4 "2022-02-10T02:34:43Z")

</div>

Well, certain unicode characters are actually treated as operators in Julia:

```julia
julia> ~(f,x)=f(x)
~ (generic function with 1 method)

julia> sin~1
0.8414709848078965

julia> ↦ = |>
|> (generic function with 1 method)

julia> 1↦sin
0.8414709848078965

julia>

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [February 10, 2022, 2:42am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/5 "2022-02-10T02:42:26Z")

</div>

You can find those operators here: [julia/julia-parser.scm at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm)

Because superscripts are already allowed for variable names, making them act as exponents (or ᵀ for transpose) would be a breaking change and would violate semantic versioning if done prior to v2.0. At its [current stage of development](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872), Julia is unlikely to add new syntax sugar except in cases where it _does not_ cause a breaking change. As demonstrated by Python 2 → Python 3 migration hell, language developers need to be very conservative with breaking changes to limit the cost they impose on users.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 10, 2022, 3:51am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/6 "2022-02-10T03:51:47Z")

</div>

well yeah if you use it like that:

```julia
julia> x²(x) = sqrt(x)
x² (generic function with 1 method)

julia> x²(3)
1.7320508075688772

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [February 10, 2022, 5:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/7 "2022-02-10T05:22:43Z")

</div>

It is possible to use unicode superscripts as postfix operators if they are prefixed by the `'` symbol.

For example:

```julia
julia> x'² = x^2
'² (generic function with 1 method)

julia> 3'²
9

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 10, 2022, 6:47am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/8 "2022-02-10T06:47:00Z")

</div>

A somewhat common pattern is to use superscripts for intermediate results of the corresponding operation, such as

```julia
x² = x^2
y = (5 + x²)^3 + 4x² + 1

```

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [March 2, 2022, 12:52am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/9 "2022-03-02T00:52:18Z")

</div>

Very interesting! Is this mentioned in the documentation?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 2, 2022, 3:30am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/10 "2022-03-02T03:30:22Z")

</div>

I would like to appeal to all Julia developers: please avoid using Unicode  
characters in your code. They are a pain to write, often are confusing  
and lead to reduced legibility (is that an apostrophe or a prime?), and  
in some settings are simply not displayed correctly.

Reference: [Commonly Confused Characters](https://www2.cs.sfu.ca/~ggbaker/reference/characters/)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 2, 2022, 4:09am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/11 "2022-03-02T04:09:13Z")

</div>

my 2c is the further the code is from a “end-users script”, the less Unicode characters it should contain. Imagine Julia’s Base looks like this and see who wants to make a PR now:

```julia
mutable struct Dict{𝒦,𝒱} <: AbstractDict{𝒦,𝒱}
    slots::Array{UInt8,1}
    keys::Array{𝒦,1}
    vals::Array{𝒱,1}
    𝒩::Int
    count::Int
    age::UInt
    ℓ::Int # an index <= the indices of all used slots
    maxprobe::Int
end

```

I wish I was dreaming but I know one package who’s code looks like this through the entire repository.

Btw, I think Keno is kind enough that he won’t mind if I use his amazing Diffractor as example, but:  
[https://github.com/JuliaDiff/Diffractor.jl/blob/bc22ad68a59b2f013d9f934de00bb7a2c1edf605/src/interface.jl#L22-L35](https://github.com/JuliaDiff/Diffractor.jl/blob/bc22ad68a59b2f013d9f934de00bb7a2c1edf605/src/interface.jl#L22-L35)

I’m torn apart on this, on one hand math symbol is extremely useful abstraction for this domain, on the other hand…do you expect users to type `∇()` instead of `gradient()` when using this package? what if they don’t have editor support (i.e. learning, following a tutorial, etc.)

---

<div class="post-metadata">

**Author:** ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)\
**Post date:** [March 2, 2022, 4:46am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/12 "2022-03-02T04:46:58Z")

</div>

I think there is an important distinction to make between using unicode in source code vs. forcing users to use unicode in an external interface. I find using unicode in source code makes it easier for me to read and develop, since it helps me avoid using unnecessarily long variable names. However I don’t make interfaces that force users to use unicode (for example we have a function called `delta` but those who want to can use the syntax `δ`).

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [March 2, 2022, 5:56am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/13 "2022-03-02T05:56:52Z")

</div>

I totally agree.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 2, 2022, 7:58am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-x-to-mean-taking-square-x-2/76137/14 "2022-03-02T07:58:19Z")

</div>

[Here](https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names)’s a link to the documentation on allowed names. (It does not mention `'` specifically, but in general it is possible to add sub/superscripts to operators.)
