<p>ECMAScript and C have a totally different nature. Certain syntactic features are cryptic in C and don't fit exactly into ECMAScript dialects.</p>
<p>For example, I don't think the following:</p>
<pre class="lang-c prettyprint-override"><code>#if FOO == &quot;bar&quot;
//
#else
//

void** k;
</code></pre>
<p>would fit into ECMAScript.</p>
<p>When I implemented a parser for ActionScript 3, I was wanting to support new syntactic features. ActionScript 3 already supported conditional compilation like follows (not part of the spec., but part of Flex compilers):</p>
<ul>
<li><strong>PREFIX :: LOCAL_NAME</strong> <em>AnnotatableDirective</em></li>
<li><strong>PREFIX :: LOCAL_NAME</strong> <em>Block</em></li>
</ul>
<p>Automatically, Flex compilers also expand inline constants matching <strong>PREFIX :: LOCAL_NAME</strong> by filtering lexical, qualified identifiers, so these <strong>PREFIX :: LOCAL_NAME</strong> constants may also be used as normal expressions.</p>
<p>However, for cases where the user wants a bit more of expressivity and be able to do more with these <code>PREFIX::LOCAL_NAME</code> constants, it's right to support conditional branches that expand code conditionally taking the same <em>parent</em> scope and <em>parent</em> parser context.</p>
<p>In Rust, for such, people use the <code>cfg_if</code> macro from the <code>cfg_if</code> crate, for conditional compilation using conditional branches:</p>
<pre class="lang-rust prettyprint-override"><code>cfg_if! {
    if #[cfg(debug_assertions)] {
        //
    } else {
        //
    }
}
</code></pre>
<p>Fine, but what is the best way to design this in ActionScript 3 or ECMAScript?</p>
<p>I've thought of:</p>
<pre><code>configuration {
    if (BLADE::MAJOR_VER &gt;= 10) {
        //
    } else {
        //
    }
}
</code></pre>
<p>(... where conditions are verified as compile-time Booleans.)</p>
<p>I'm not sure whether this is nice, but if you compare it to E4X, it makes some sense. E4X supported things like:</p>
<ul>
<li><code>default xml namespace = Namespace_obj;</code></li>
<li><code>for each</code></li>
</ul>
<p>I'd like to change it to make it more concise if possible, but I can't figure out any other better syntax. I could maybe reduce the <code>configuration</code> keyword to <code>config</code>.</p>
<p>I'm not sure whether the hashword would be nice in AS3 (looks cryptic); that's why I got a Rust-like variant, just using a less cryptic form.</p>
<p>Is there a better art for this?</p>

<language-design><design-choice-comparison><conditional>
Designing conditional compilation blocks
CC BY-SA 4.0
2024-05-29T13:28:10.133Z
I think it's necessary to distinguish between conditional compilation and dead code elimination when an `if` condition is a compile-time constant. Any compiler can do the latter without requiring any special syntax for the programmer to indicate which conditions are compile-time constants (the compiler can determine that for itself). So if `BLADE::MAJOR_VER` is a constant which the compiler can know the value of, this is a non-problem. On the other hand, C's conditional compilation allows things like declaring a variable with one or another type, linking to one or another library, etc.
kaya3
CC BY-SA 4.0
2024-05-29T16:52:41.370Z
I don't really understand the reasoning that C pre-processor syntax "wouldn't fit into JavaScript" - it doesn't "fit" with C either, it's basically a completely separate text processing language. That somewhat goes with kaya3's comment - having a completely distinct syntax lets you have a more powerful / low-level pre-processor, e.g. the conditional block can contain un-balanced braces or code that would completely choke the parser in some versions. The same thinking leads to the concept of "conditional comments": something that looks like a block comment to some compilers, but code to others.
IMSoP
CC BY-SA 4.0
2024-05-29T17:01:34.660Z
Also, the Rust [`cfg_if!` macro](https://docs.rs/cfg-if/latest/cfg_if/) isn't actually doing conditional compilation itself, it's just rewriting source code to attach `cfg` attributes to a bunch of declarations or statements, which are the [actual conditional compilation feature of the language](https://doc.rust-lang.org/reference/conditional-compilation.html). Again, that leads to quite a different concept of what conditional compilation can do.
IMSoP
CC BY-SA 4.0
2024-05-29T21:16:38.423Z
@IMSoP Ah, right, I've almost no experience with preprocessors and totally forgot that C's preprocessors are more than what Rust's conditional compilation offers. TBH I didn't consider C preprocessors could influence the tokenizer in anyway; I thought they just influenced the parser and semantics.
Hydroper
CC BY-SA 4.0
2024-05-31T03:01:56.520Z
I have seen `when` used on analogy to `if` (and functioning similarly with `else`) for compile-time conditionals.
apropos
# Designing conditional compilation blocks
Posted by: username_0 on 2024-05-29
ECMAScript and C have a totally different nature. Certain syntactic features are cryptic in C and don't fit exactly into ECMAScript dialects.
For example, I don't think the following:
#if FOO == "bar"
//
#else
//

void** k;

would fit into ECMAScript.
When I implemented a parser for ActionScript 3, I was wanting to support new syntactic features. ActionScript 3 already supported conditional compilation like follows (not part of the spec., but part of Flex compilers):

PREFIX :: LOCAL_NAME AnnotatableDirective
PREFIX :: LOCAL_NAME Block

Automatically, Flex compilers also expand inline constants matching PREFIX :: LOCAL_NAME by filtering lexical, qualified identifiers, so these PREFIX :: LOCAL_NAME constants may also be used as normal expressions.
However, for cases where the user wants a bit more of expressivity and be able to do more with these PREFIX::LOCAL_NAME constants, it's right to support conditional branches that expand code conditionally taking the same parent scope and parent parser context.
In Rust, for such, people use the cfg_if macro from the cfg_if crate, for conditional compilation using conditional branches:
cfg_if! {
    if #[cfg(debug_assertions)] {
        //
    } else {
        //
    }
}

Fine, but what is the best way to design this in ActionScript 3 or ECMAScript?
I've thought of:
configuration {
    if (BLADE::MAJOR_VER >= 10) {
        //
    } else {
        //
    }
}

(... where conditions are verified as compile-time Booleans.)
I'm not sure whether this is nice, but if you compare it to E4X, it makes some sense. E4X supported things like:

default xml namespace = Namespace_obj;
for each

I'd like to change it to make it more concise if possible, but I can't figure out any other better syntax. I could maybe reduce the configuration keyword to config.
I'm not sure whether the hashword would be nice in AS3 (looks cryptic); that's why I got a Rust-like variant, just using a less cryptic form.
Is there a better art for this?
**Tags:** language-design design-choice-comparison conditional
> **Comments:**
> **username_1**: I think it's necessary to distinguish between conditional compilation and dead code elimination when an `if` condition is a compile-time constant. Any compiler can do the latter without requiring any special syntax for the programmer to indicate which conditions are compile-time constants (the compiler can determine that for itself). So if `BLADE::MAJOR_VER` is a constant which the compiler can know the value of, this is a non-problem. On the other hand, C's conditional compilation allows things like declaring a variable with one or another type, linking to one or another library, etc.  
> **username_2**: I don't really understand the reasoning that C pre-processor syntax "wouldn't fit into JavaScript" - it doesn't "fit" with C either, it's basically a completely separate text processing language. That somewhat goes with kaya3's comment - having a completely distinct syntax lets you have a more powerful / low-level pre-processor, e.g. the conditional block can contain un-balanced braces or code that would completely choke the parser in some versions. The same thinking leads to the concept of "conditional comments": something that looks like a block comment to some compilers, but code to others.  
> **username_2**: Also, the Rust [`cfg_if!` macro](https://docs.rs/cfg-if/latest/cfg_if/) isn't actually doing conditional compilation itself, it's just rewriting source code to attach `cfg` attributes to a bunch of declarations or statements, which are the [actual conditional compilation feature of the language](https://doc.rust-lang.org/reference/conditional-compilation.html). Again, that leads to quite a different concept of what conditional compilation can do.  
> **username_0**: @IMSoP Ah, right, I've almost no experience with preprocessors and totally forgot that C's preprocessors are more than what Rust's conditional compilation offers. TBH I didn't consider C preprocessors could influence the tokenizer in anyway; I thought they just influenced the parser and semantics.  
> **username_3**: I have seen `when` used on analogy to `if` (and functioning similarly with `else`) for compile-time conditionals.  
---
## 0 answers
