Sigils in Nu, and a question

Monday, 03 Sep 2007

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)))) 
Comments (9) post a reply
  1. Mr eel Monday, 03 Sep 2007, 10:43 PM PDT

    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 Monday, 03 Sep 2007, 11:06 PM PDT

    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 Monday, 03 Sep 2007, 11:22 PM PDT

    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 Tuesday, 04 Sep 2007, 03:50 AM PDT

    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 Tuesday, 04 Sep 2007, 07:24 AM PDT

    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 Tuesday, 04 Sep 2007, 09:59 AM PDT

    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 Tuesday, 04 Sep 2007, 08:53 PM PDT

    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 Tuesday, 04 Sep 2007, 09:14 PM PDT

    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 Wednesday, 05 Sep 2007, 06:08 PM PDT

    How about question mark (?x) ?