Using `it` as a Placeholder in JavaScript Pipeline Operator
## Motivation
The current Hack-style pipeline proposal uses `%` as a placeholder for the left-hand value of `|>`. While functional, `%` has readability issues and conflicts with the modulo operator. This proposal suggests replacing `%` with `it`, which is more intuitive and avoids syntax conflicts.
## Proposal
Introduce `it` as a context-sensitive keyword in pipeline expressions. It would act as a placeholder for the left-hand value, similar to `yield` in generators.
### Example
```js
const result = 5 |> it * 2 |> foo(it) |> it + 3;
// Equivalent to: foo(5 * 2) + 3
```
## Design Details
1. **Contextual Scope**
- `it` is only valid inside pipeline expressions, preserving its use as a regular variable elsewhere.
- Example:
```js
let it = 42;
const result = 5 |> it * 2; // `it` is 5 inside the pipeline
console.log(it); // Still 42 outside
```
2. **Comparison to `yield`**
- Like `yield`, `it` is only special in a specific context.
- Unlike `yield`, `it` is a simple placeholder without control-flow effects.
3. **Backward Compatibility**
- Since `it` is not a reserved word, this proposal does not break existing code.
- In case of name conflicts, pipeline-scoped `it` takes precedence.
## Advantages
✅ **Improved readability**: `it` makes expressions more natural compared to symbols like `%`.
✅ **Familiar syntax**: Similar to `it` in Kotlin and Ruby.
✅ **No conflict with `%` (modulo operator)**:
```js
const result = 10 |> it % 3; // Clearly computes 10 % 3
```
## Potential Challenges
1. **Variable Name Conflicts**
- If `it` exists in the outer scope, it might cause confusion. This is mitigated by giving `it` pipeline-specific precedence.
2. **Naming Debate**
- Placeholder names are often controversial, but `it` offers a balance between clarity and compatibility.
## Comparison: `it` vs. `%`
| Placeholder | Readability | `%` Conflict | Learning Curve | Compatibility |
|------------|------------|-------------|--------------|--------------|
| `%` | Low | Yes | Requires adaptation | Compatible |
| `it` | High | No | Intuitive | Compatible |
---
## Conclusion
Replacing `%` with `it` in pipeline expressions enhances readability, avoids conflicts with the modulo operator, and maintains backward compatibility.
关闭于 2025-03-04 2 条评论