TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Help me choose the best syntax for a new language

5 pointsby ne01over 7 years ago
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 :)

3 comments

tremensover 7 years ago
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> &lt;set var=&quot;@x&quot; global&gt;3&lt;&#x2F;set&gt; &lt;scope&gt; &lt;set var=&quot;x&quot;&gt;4&lt;&#x2F;set&gt; &lt;put&gt;x&lt;&#x2F;put&gt; &#x2F;&#x2F; 4 &lt;put&gt;@x&lt;&#x2F;put&gt; &#x2F;&#x2F; 3 &lt;scope&gt; &lt;set var=&quot;x&quot;&gt;10&lt;&#x2F;set&gt; &lt;put&gt;x&lt;&#x2F;put&gt; &#x2F;&#x2F; 10 &lt;put&gt;@x&lt;&#x2F;put&gt; &#x2F;&#x2F; 3 &lt;&#x2F;scope&gt; &lt;put&gt;x&lt;&#x2F;put&gt; &#x2F;&#x2F; 4 &lt;put&gt;@x&lt;&#x2F;put&gt; &#x2F;&#x2F; 10 &lt;&#x2F;scope&gt;</code></pre>
JoshTriplettover 7 years ago
A few more alternatives:<p>- B, with an explicit syntax for accessing the &quot;global&quot; 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.
billconanover 7 years ago
I choose B and make variable overshadowing a compiler warning.