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> <set var="x">3</set>
<scope>
<set var="@x">4</set>
Local x is <put>@x</put>.
</scope>
Global x is <put>x</put>.
</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> <set var="x">3</set>
<scope>
<set var="x">4</set>
Local x is <put>x</put>
</scope>
Global x is <put>x</put>
</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't like this because there would be too many `@`.<p>D) ... Any other suggestions?<p>Thanks :)
A mix of B and C.<p>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.<p>Example:<p><pre><code> <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></code></pre>
A few more alternatives:<p>- B, with an explicit syntax for accessing the "global" x. For instance, ::x, or global::x.<p>- B, with a compile-time warning or error if you shadow a name. This would be my preferred alternative.