
Ask HN: Help me choose the best syntax for a new language - ne01
Hi HN friends,<p>I have a hard time deciding between the following options for variable scope syntax. The language is supposed to be very easy to learn.<p>A) Local variables start with `@`.<p>Pros: Easy to identify global and local variables. You have easy access to global variables inside a scope.<p>Cons: Users have to learn variable scope from the start.<p>Example:<p><pre><code>  &lt;set var=&quot;x&quot;&gt;3&lt;&#x2F;set&gt;
  &lt;scope&gt;
    &lt;set var=&quot;@x&quot;&gt;4&lt;&#x2F;set&gt;
    Local x is &lt;put&gt;@x&lt;&#x2F;put&gt;.
  &lt;&#x2F;scope&gt;
  Global x is &lt;put&gt;x&lt;&#x2F;put&gt;.
</code></pre>
Result:<p><pre><code>  Local x is 4.
  Global x is 3.

</code></pre>
B) Local variables silently overshadow existing global variables.<p>Pros: Easier to learn (no more `@` in the syntax).<p>Cons: You loose access to the global `x` -- the only way to access a global variable is to make sure it has a unique name.<p>Example:<p><pre><code>  &lt;set var=&quot;x&quot;&gt;3&lt;&#x2F;set&gt;
  &lt;scope&gt;
    &lt;set var=&quot;x&quot;&gt;4&lt;&#x2F;set&gt;
    Local x is &lt;put&gt;x&lt;&#x2F;put&gt;
  &lt;&#x2F;scope&gt;
  Global x is &lt;put&gt;x&lt;&#x2F;put&gt;
</code></pre>
Result:<p><pre><code>  Local x is 4.
  Global x is 3.
</code></pre>
C) Reversed system: use `@` for global. I personally don&#x27;t like this because there would be too many `@`.<p>D) ... Any other suggestions?<p>Thanks :)
======
tremens
A mix of B and C.

Variables bound within a scope should be able to shadow variables bound in
parent scopes, but global vars should be made obvious when they are used
within the codebase - visually marking global variables is a good thing.

Example:

    
    
      <set var="@x" global>3</set>
      <scope>
    
          <set var="x">4</set>
    
          <put>x</put> // 4
          <put>@x</put> // 3
    
          <scope>
             <set var="x">10</set>
    
             <put>x</put> // 10
             <put>@x</put> // 3
          </scope>
    
          <put>x</put> // 4
          <put>@x</put> // 10
      </scope>

------
JoshTriplett
A few more alternatives:

\- B, with an explicit syntax for accessing the "global" x. For instance, ::x,
or global::x.

\- B, with a compile-time warning or error if you shadow a name. This would be
my preferred alternative.

------
billconan
I choose B and make variable overshadowing a compiler warning.

