n2liquid's sandbox

In Soviet Russia, the keywords conflict with your variables

Posted on: August 16, 2011

I’ve always loathed having to prefix my variables with dollar signs in languages like PHP. I guess they decided this so that you’d be free to name your variables the way you wanted without conflicting with language keywords.

I dislike how the code looks like, cluttered with $’s everywhere. I noticed, though, that inverting the game could solve my issues — if we had a language where the keywords were prefixed instead, an aware source editor could remove the prefix from visualization and at the same time style the identifier as a keyword. Easy syntax highlighting and the best of both worlds: no conflicts and no (visible) prefixes. The only problem is that the source would look more cluttered than usual in an unaware editor.

In case you’re wondering why this couldn’t be done with variable name prefixes, that’s because simply removing the prefix would give no visual clue that it’s there, which is bad (if you forget it, you can’t see it’s missing since the variable name would look the same as if you didn’t). Moving the prefix somewhere in which its presence makes some other visual difference works, though, because, in case you forget prefixing it the syntax won’t color up.

Following this same reasoning, I noticed two dollar signs could be used for typenames, in a way that keywords, typenames, and variable names all have separate namespaces, can’t clash with each other and greatly simplify syntax highlighting.

Thus, something like:

$var buzzer = $$buzzer(2); // creates variable ‘buzzer’ and assigns an instance of type ‘buzzer’


var buzzer = buzzer(2); // creates variable ‘buzzer’ and assigns an instance of type ‘buzzer’

Naturally, you can also name your variable ‘var’ without a problem:

$var var; /* becomes: */ var var;

I’ll consider using this on Chii.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cute bouncing pixels


Twitter (technical)

Error: Please make sure the Twitter account is public.

Twitter (personal)

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Get messaged when I post something new!
Just enter your e-mail and hit Follow:

Join 171 other followers

%d bloggers like this: