Swift fun fact #1: You can use emoji characters in variable, constant, function, and class names

by Joey deVilla on June 3, 2014

swift kickBy allowing Unicode characters in constant and variable names, Apple’s new Swift programming language will allow programmers whose native languages don’t use the Roman alphabet to write code that makes more sense to them. Since emoji are part of the Unicode set, expect to see amusing code demos and search-and-replace pranks that look like this:

poopy swift code example

(And yes, this code compiles and runs on Swift using the XCode 6 beta.)

{ 16 comments… read them below or add one }

1 David Balažic June 3, 2014 at 8:10 am

If it is Unicode, why not post it as text?
(of course so I can copy and paste it into my current project !! muha ha ha ha…)

2 Ilmari Kontulainen June 3, 2014 at 10:27 am

This is actually brilliant. It can help teaching programming to young people.

3 Joey deVilla June 3, 2014 at 11:49 am

David Balažic: I tried to paste it into the blog entry, but it got mangled into those non-descript box characters as cut-and-pasted Unicode often is.

4 Achraf Almouloudi June 4, 2014 at 2:14 am

Stupid and useless, except for teaching programming to .. uh .. kids. Professional and experienced programmers know Roman characters and if you haven’t noticed yet, they have been using them to program everything till today, iCrap invention.

5 Eric D June 4, 2014 at 6:38 am

In Java, only letters and numbers are accepted, in any unicode alphabet. This means that emoticons, symbols and such are refused. But you can use the upside-down unicode character converter.

6 Ejder Yurdakul June 4, 2014 at 10:27 am

There is only one question remaining, why? Why would i check a variable to make sure if it’s a :) or kinky ;). Is this a teddy bear or a glass of wine. Totally useless but hey, it is apple, the new mirosoft!

7 Jason Chiang June 4, 2014 at 12:18 pm

Actually PHP can do it, too. I just made a sample from your code.
I have never thought about coding in emoji.
Anyway, it’s fun though….

see demo below.
http://tie.tw/emoji/emoji.php

8 Jason Chiang June 4, 2014 at 12:20 pm

Please make sure you view source code in Safari for proper emoji display.
http://tie.tw/emoji/emoji.php

9 Morgan June 4, 2014 at 8:40 pm

Emoji is a bit silly, but the ability to use unicode characters opens a lot of doors - particularly if your first language isn’t english.

10 Real Time June 4, 2014 at 11:50 pm

The only new thing about this is that “non-script” characters like Emoji can be used for identifiers (but why?). Unicode is valid in existing programming languages like Go:
http://play.golang.org/p/5D_1jJocd5

11 Real Time June 4, 2014 at 11:53 pm

For some reason, the Go playground snippet link does not work when pasted into this comment system (but it works if directly pasted in to the URL bar). So, here is the code:
// You can edit this code!
// Click here and start typing.
package main

import “fmt”

func 世界() {
fmt.Println(“Hello,

12 Maynard June 5, 2014 at 1:00 am

This looked dumb and pointless, but the emoji part is not important. What IS important is the ability to use more than just ASCII.
For the domain I care about, math/physics, it means the ability to use variables names that are greek (and perhaps to be even more ambitious, we shall see — maybe a variable named ∫α rather than integralOfAlpha?)
Another domain where I could imagine this to be useful is code that is either targeting a foreign language, or targeting somehow translations between two languages. I’m sure if you’re Chinese, for example, you’d prefer to have your variable names in Chinese…

13 Jan Hudec June 5, 2014 at 1:39 am

My former colleague worked for Japanese company. In Europe. The code they took over had comments in Japanese. You can probably imagine it wasn’t very helpful. Now consider what would happen if it had identifiers in Japanese as well.

14 Qoheleth June 5, 2014 at 5:25 pm

Yeah, the real benefit of this is that you can program in Russian or Vietnamese or any other non-latin1 language and have the compiler understand you. The ability to use ridiculous Unicode dingbats is just an amusing side effect.

15 Joey deVilla June 5, 2014 at 7:52 pm

Everybody, take note of the first sentence in the article:

By allowing Unicode characters in constant and variable names, Apple’s new Swift programming language will allow programmers whose native languages don’t use the Roman alphabet to write code that makes more sense to them.

The emoji names are just a bonus for amusing demos.

16 Daniel Lo Nigro June 8, 2014 at 6:21 pm

A lot of languages allow unicode characters as identifiers. This is nothing new.

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: