Unicode Support in Programing Language Function Name and Operator

By Xah Lee. Date: . Last updated: .

Unicode Support in Function Name and Operator

The question we want to ask are, does your favorite programing language:

  1. Support literal Unicode character in strings? (almost all programing language today (as of 2019) support unicode in string or source code.)
  2. Support literal Unicode character in source code? (example: in comment.)
  3. Allow Unicode character in identifier (variable or function names), e.g. Ο† = 3; Ο†(3).
  4. Allow Non-letter Unicode character in identifier, e.g. βŠ•(v1, v2).
  5. Allow defining new operators e.g. x +++ y.
  6. Allow defining new operators using unicode symbol e.g. M βŠ— v.

Here is a table showing support of unicode in identifier, and defining operators.

Unicode in Identifier and Operator
languageallow unicode in identifierallow math symbol in identifierallow define operatorallow math symbol in operator
Cnononono
C++nononono
Goβœ…nonono
Javaβœ…nonono
JavaScriptβœ…nonono
Pythonβœ…noβœ…no
Perlβœ…nonono
Rubyβœ…βœ…?no
ocamlnonoβœ…no
Haskellβœ…noβœ…βœ…
Emacs Lispβœ…βœ…nono
Juliaβœ…βœ…βœ…βœ…
Wolfram Langβœ…βœ…βœ…βœ…
Rust????
Nimβœ…no??

What Characters Are Unicode Letter

Many languages do not allow non-letter e.g. Unicode: Math Symbols βˆ‘ ∫ π² ∞ in identifier names. e.g. JavaScript, python, golang, java.

Why Define Operators?

Golang

Identifier must start with a letter (including unicode letter), followed by letter or digit.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

package main

import "fmt"

func main() {

	var Ξ± = 3

	// var tβŠ• = 4
	// syntax error
	// invalid identifier character U+2295 'βŠ•'

	fmt.Printf("%v\n", Ξ±)
}

Python 2

Python 2.x does not support Unicode char for variable or function names.

Python 2.x's Unicode support is not very good. But does work for processing Unicode in string.

See: Python: Unicode Tutorial 🐍 .

Python 3

Python 3 supports Unicode in variable names and function names.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

# python 3


def Ο†(n):
    return n + 1

Ξ± = 4
print(Ο†(Ξ±))  # prints 5
# python 3

# β™₯ = 4

#     β™₯ = 4
#     ^
# SyntaxError: invalid character in identifier

JavaScript

JavaScript supports Unicode in variable name and function name.

JavaScript identifiers (variable or function names) must begin with a letter, underscore _, or a dollar sign $. The β€œletter” or β€œdigit” includes non-ASCII Unicode that are also letters or digits.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

// -*- coding: utf-8 -*-

var ζ„› = "β™₯";

function Ξ»(n) {
  return n + "β™₯";
}

alert(Ξ»(ζ„›));

As of , all browsers support it.

Ruby

Ruby has robust support of Unicode, starting with version 1.9. (2007)

Unicode can be in variable or function names.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

# -*- coding: utf-8 -*-
# ruby

β™₯ = "β™₯"

def Ξ» n
  n + "美"
end

p (Ξ» β™₯) == "β™₯美" # true

[Ruby: Unicode Tutorial πŸ’Ž]

Perl

Perl, since about 2010, has good Unicode support. Unicode can be in variable or function names.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

The exact rule is complicated. But basically, if the unicode is considered a letter, then, it's ok.

# -*- coding: utf-8 -*-
# perl 5.14

use strict;
use utf8; # necessary if you want to use Unicode in function or variable names

# string with unicode char
my $s = 'I β˜… you';
$s =~ s/β˜…/β™₯/;
print "$s\n";

# var with Unicode char
my $Ξ² = 4;
print "$Ξ²\n";

# function with Unicode char
sub Ξ» { return 2;}
print Ξ»();
# -*- coding: utf-8 -*-
# perl v5.32.1
# 2022-12-28

use strict;
use utf8;

# version string
print $^V;
# sample output: v5.32.1

# ssss---------------------------------------------------

# identifier cannot be arbitrary unicode char

# my $πŸ˜‚ = 3;
# error
# Unrecognized character \x{1f602}

# ssss---------------------------------------------------

use strict;
use utf8;

# identifier cannot be arbitrary unicode char

# my $β™₯ = 3;
# error
# Unrecognized character \x{2665}

Java

Java supports Unicode fully, including use in variable/class/method names.

Unicode math symbols NOT allowed (e.g. βˆ‘ βŠ• Β°).

class ζ–Ή {
    String εŒ— = "north";
    double Ο€ = 3.14159;
}

class UnicodeTest {
    public static void main(String[] arg) {
        ζ–Ή x1 = new ζ–Ή();
        System.out.println( x1.εŒ— );
        System.out.println( x1.Ο€ );
    }
}

Emacs Lisp

For text processing, the most beautiful lang with respect to Unicode is emacs lisp. In elisp, you don't have to declare none of the Unicode or encoding stuff. You simply write code to process string or files, without even having to know what encoding it is. Emacs the environment takes care of all that.

Emacs Lisp allow Unicode in var/function names.

Non-letter unicode math symbol allowed.

(defun β™₯ ()
  "Inserts stuff"
  (interactive)
  (let ((Ξ± "β™₯ ζ„› ☯"))
    (insert Ξ±)))

(to try the above in emacs: paste the above into a empty file, then select it, then Alt+x eval-region to make emacs eval it. Now, you can press Alt+x then type β™₯ (just copy paste), it'll insert β€œβ™₯ ζ„› β˜―β€.) (See: Emacs and Unicode Tips β€’ Emacs Lisp Basics.)

Ocaml

Ocaml does not allow any non-ascii character in names.

Ocaml can define operators, but operators can only be some ASCII chars.

ocaml identifier allowed chars 2019-06-21 fmxgy
ocaml identifier allowed chars 2019-06-21

source https://caml.inria.fr/pub/docs/manual-ocaml/names.html#operator-name

ocaml operator allowed chars 2019-06-21 pnbsv
ocaml operator allowed chars 2019-06-21
ocaml operator allowed chars 2019-06-21 8wy5n
ocaml operator allowed chars 2019-06-21

Haskell

Haskell allows non-ascii char in variable or function names. but it must be Unicode letter char. Math symbol not allowed.

Haskell can define operator, and the operator can be any unicode character in the category of symbol or punctuation.

Summary: variable or function names must be unicode letter, and operator must be unicode symbol.

haskell lex 2019-06-21 8shnc
haskell lex 2019-06-21

source https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-180002.4

haskell operator unicode 2019-06-21 mfyyv
haskell operator unicode 2019-06-21

Here is haskell user defined operators in action:

haskell snowman operator h5m4n
Haskell snowman and mountain operators [image source https://twitter.com/Iceland_jack/status/1142043958299811840]

Julia

Julia allow Unicode math symbols variable names and also allow defining operators with math symbols.

julia unicode symbol name 2019-06-20 y2j8g
[https://docs.julialang.org/en/latest/manual/variables/#Allowed-Variable-Names-1]

Wolfram Language

Wolfram Language unicode 2021-07-21
Wolfram Language unicode 2021-07-21

Technically, Mathematica source code is ASCII. Characters in Unicode or Mathematica's own set of math symbols are represented by a markup, much like HTML entities. However, Mathematica editor (the Front End) displays it rendered, and there's robust system for user to input math symbols.

Linden Scripting Language (Second Life)

Linden Scripting Language supports Unicode in function or variable names.

string aβˆ‘β™₯ = "variable with Unicode char in name";

string tβˆ‘β™₯() { return "function with Unicode char in name";}

default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }

    touch_start(integer num_detected)
    {
        llSay(0, (string) tβˆ‘β™₯() + "; " + (string) aβˆ‘β™₯);
    }
}

See:

Fortress

APL etc

APL is well-known for its use of math symbols. [see APL Symbols Meaning and Code Example] but am not sure if it allows unicode symbol in identifiers or defining operator.

For other programing language language's support of unicode in names, see http://rosettacode.org/wiki/Unicode_variable_names Note: that page does not discuss if math symbol character can be used.

Why Use Unicode in Variable Names?

Languages and Unicode Support History

thanks to boostjam on perl

thanks to Hleb Valoshka on ruby. [https://plus.google.com/b/113859563190964307534/105354689506653311797/posts]

Programing Language Operators