Sigils in Nu, and a question

I’d like to get some feedback on at least one aspect of Nu before anyone else becomes dependent on it: the use of sigils to indicate the type of a name binding.

As I’ve described Nu, it uses special prefixes on names to signal how they are bound. The ones I chose were stolen directly from Ruby (talent imitates, genius steals), and are as follows:

$ indicates a global variable.
@ indicates an instance variable.
@@ indicates a class variable (not yet, but likely).


I think those are probably all fine, and if anyone doesn’t like them, I can always blame Matz :-). But I added one more, and I’m having second thoughts about my choice:

__ indicates a gensym.


I’ve been having doubts about this after revisiting some python code, where double-underscores are used all over the place, and after considering that underscores and double-underscores show up a lot in compiled C object files. So to avoid ambiguity, I’d like to pick something completely different to be the gensym sigil. My current favorite alternative is to use a two-caret prefix, making ^^foo a gensym.

Any comments or other suggestions?

p.s. A gensym is a “generated symbol” that is used in a macro expansion to avoid conflicts between names. In the macro below, __x is a gensym name. Each time the factorial macro is expanded, the gensym name is replaced with an automatically-generated name that is guaranteed to be unique.

(macro factorial    
     (set __x (eval (car margs)))
     (if (== __x 0) 
         (then 1)
         (else (* (factorial (- __x 1)) __x)))) 

9 comments ↓

#1 Mr eel on September 03, 2007 at 22:43 GMT

I like the Ruby variable prefixes, so no complaints from me.

As for the gensym, how about a hash as a prefix? Assuming that doesn’t conflict with anything.

x

It’s only one character, which is nicer than the two caret prefix I think.

#2 Tim on September 03, 2007 at 23:06 GMT

That big x is out-of-control Textile markup.

Here’s a hash-x: #x.

I think it’s a bit too close to this: #{x}, which if placed inside a string will cause the value of x to be interpolated into the string.

But side by side, maybe #x does beat ^^x.

#3 Tim on September 03, 2007 at 23:22 GMT

Oops, a hash is a comment character, which is useful when it’s time to write Nu scripts that begin with:

#!/usr/bin/env nush
#4 Matt Mower on September 04, 2007 at 03:50 GMT

Yeah I’m fine with the Ruby sigils. I also think, given the limited context, that either _ or ^^ is a reasonable choice for gensyms although looking at both in textmate I think I find _ to be slighter easier on the eye.

(p.s. I couldn’t seem to escape those underscores properly to avoid textile formatting).

#5 Phil Toland on September 04, 2007 at 07:24 GMT

The Ruby sigils are fine. I personally prefer the double underscores to ^^ or # for gensyms. I wouldn’t worry about double underscored names in C bleeding into Nu via FFI. IIRC, the double underscore is reserved for implementers of the standard library and should never be a part of a C library’s public interface.

#6 Austin on September 04, 2007 at 09:59 GMT

Why not reuse an existing sigil, $—e.g., $$ means a gensym. Presumably you’re not using $$ to mean PID like Ruby does.

#7 Mr eel on September 04, 2007 at 20:53 GMT

Textile!

Well, if a hash prefix is out, then the double dollar sign is the go.

It’s immediately recognisable and doesn’t clash with anything else. Does it imply a relationship to the global variables though? Much like the double @ in a class variable implies a relationship to an instance variable.

#8 Tim on September 04, 2007 at 21:14 GMT

One thing that I don’t like about the double dollar sign is its lack of a relationship with the single dollar sign. So since no alternative seems any better, I’m going to stick with the Textile-unfriendly __ (To beat Textile, wrap it in <code> tags).

Thanks for all of your comments on this.

#9 eric on September 05, 2007 at 18:08 GMT

How about question mark (?x) ?

Leave a Comment (sign in with Twitter)