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.)

Want to see more Swift content?

Click here or on the “Swift Kick” logo near the top of this article to see all the Swift-related articles on Global Nerdy.

{ 26 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.

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

Please make sure you view source code in Safari for proper emoji display.

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:

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 世界() {

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.

17 Sony Ynos August 15, 2014 at 5:57 am

Yeah, and I’m working on Japanese project, in which they name variables by JAPANESE.

18 Warren October 20, 2014 at 12:28 pm

New challenge:
Write code that tells a funny story with emoji variable names that has nothing to actually do with what is going on in the code.
Mind = Blown.

19 gistya November 21, 2014 at 11:49 am

You should define a custom exception class and call it (emoji for turd). Throw new (turd) … epic.

20 Dan April 17, 2015 at 1:44 pm

Nice to see other languages catching up to what Perl has had for a long time.

21 emily April 17, 2015 at 8:56 pm

I can see it being used for code obfuscation, too.

22 Hasan July 22, 2015 at 12:30 pm
23 Ryan November 20, 2015 at 12:05 pm

It is a pretty stupid feature, but from a language design standpoint, if you are going to allow other unicode characters for localization then it would be more difficult and error prone to remove emoji support than leave it in. That makes the decision an easy on because it also provides some serious comic relief.

24 Chris January 24, 2016 at 2:02 am

if (.⚡() < .5 && !(.()) {.(); .⚠(" = ⏰!"); }

I could get used to this.

25 Kaivosukeltaja February 5, 2016 at 4:32 am

Great, looking forward to my first encounter with outsourced code using हिन्दी variable and function names.

26 Miranda June 30, 2016 at 3:04 pm

I wanna ask Jason Chiang something! My question is:
how did you made your sample that you made from the original code? I wonder because Im searching for a way to add emojis in php variables!

Leave a Comment

{ 14 trackbacks }

Previous post:

Next post: