Categories
Swift Kick

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

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.

42 replies on “Swift fun fact #1: You can use emoji characters in variable, constant, function, and class names”

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

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.

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.

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.

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!

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.

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,

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…

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.

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.

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.

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

[…] Even gathering the information required to make an informed decision is almost certainly counterproductive. If, before embarking on a project, you actually took the time to analyze all the possibilities, and then climbed the resulting learning curve, you’d promptly get beaten to market by some teenager using PHP and Swift with emoji variable names– […]

[…] Even gathering the information required to make an informed decision is almost certainly counterproductive. If, before embarking on a project, you actually took the time to analyze all the possibilities, and then climbed the resulting learning curve, you’d promptly get beaten to market by some teenager using PHP and Swift with emoji variable names– […]

[…] Even gathering the tips required to make an informed determination is almost undoubtedly counterproductive. If, before embarking on a venture, you actually took the time to research the entire prospects, after which climbed the ensuing finding out curve, you’d at once get beaten to market through some teen the use of PHP and Swift with emoji variable names– […]

[…] Even gathering the information required to make an informed decision is almost certainly counterproductive. If, before embarking on a project, you actually took the time to analyze all the possibilities, and then climbed the resulting learning curve, you’d promptly get beaten to market by some teenager using PHP and Swift with emoji variable names[3]– […]

[…] Even gathering the information required to make an informed decision is almost certainly counterproductive. If, before embarking on a project, you actually took the time to analyze all the possibilities, and then climbed the resulting learning curve, you’d promptly get beaten to market by some teenager using PHP and Swift with emoji variable names– […]

[…] Even gathering the tips required to make an knowledgeable choice is almost for sure counterproductive. If, prior to embarking on a venture, you if truth be told took the time to research the entire prospects, after which climbed the ensuing studying curve, you’d instantly get beaten to market through some teen the use of PHP and Swift with emoji variable names– […]

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.

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.

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

Hello!
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!

Comments are closed.