proposal-pipeline-operator icon indicating copy to clipboard operation
proposal-pipeline-operator copied to clipboard

Bikeshedding the Hack topic token

Open js-choi opened this issue 7 years ago • 815 comments

This issue is for bikeshedding the spelling of the topic reference’s token in Hack pipes, branching off from https://github.com/tc39/proposal-pipeline-operator/issues/75#issuecomment-362850526.

The table has its own editable page on the wiki. Please read this table first before contributing to this issue.

Please also keep discussion on topic: bikeshedding the topic reference. For other topics, search for other existing issues. Thank you!

Old obsolete questions

For more context, see the Hack pipes proposal and the wiki home page.

These currently are the most topical bikeshedding questions that I see now:

  1. What is the optimal tradeoff in writability (easily typed ASCII soup, e.g., ?, or easily typed privileged valid variable, e.g., $) versus readability (less easily inputted non-ASCII syntax, e.g., )?

  2. Related to question 2: Can non-ASCII Unicode syntax characters be considered for the pipe placeholder, or must they be categorically excluded?

    A list of all possible Pattern_Syntax Unicode characters is available.

  3. If question 3’s answer is that non-ASCII Unicode syntax may be excluded, which non-ASCII syntax character would be visually understandable and/or less difficuldif­fi­cultt?

    Many non-ASCII symbols are easily inputted in certain OSes. For instance, in macOS, several dozen typographic characters are directly typable using Option or Option + Shift (image of keyboards with various active modifier keys via Macworld article. It may be worth determining if there is an intersection of these easily typable non-ASCII characters across the default keyboard layouts of many OSes.

  4. For nullary operators vs. valid variable identifiers: How important is it that the placeholder be statically analyzable?

    A nullary operator can be always be statically recognized in the RHS. In contrast, a valid identifier can be statically recognized only if the rule is: “Tacit function calling may occur only when the RHS is a bare identifier, rather than allowing the RHS to be any arbitrary expression without a placeholder.”

  5. Operator vs. identifier again: How often would someone want to use an identifier from an outer lexical context of the same name as the pipe placeholder?

    If the placeholder is $, how often would a programmer want to use jQuery’s $ or another externally defined $ in a pipe’s RHS, as well as the pipe placeholder? How often would they be surprised if they could not, without defining a dummy variable for the outer $? How bad of a footgun (i.e., bug by programmer-unexpected behavior) would this be?

  6. How much should the pipe placeholder stay compatible with @rbuckton’s higher-order functional operators, which possibly would use {…}?

  7. @rbuckton proposed a partial-application placeholder that may be explainable by multiple topic placeholders. In other words, the pipeline syntax might be completely unifiable with the PA syntax with the right enhancements.

    Assuming a future shorthand “pipe-function operator” like +> (which would be an abbreviation for x=>x|>), then @rbuckton’s proposal’s f(?, 3) would instead be +> f(?, 3), which in turn would be shorthand for x=>x|> f(?, 3).

    Creating non-unary functions could be done by adding numbers to placeholders, such as ?0, ?1, ?2, etc.
    For instance, example.sort(+> ?0 - ?1)
    would mean example.sort((x, y) => x - y).
    (?0 would be equivalent to plain ?.)

    Which placeholders would have any problems with staying forward compatible with such a future proposal?

As of 2025-04-07, I most like #, followed by ^^ or ##, then more distantly #_, @@, and %%.

js-choi avatar Feb 04 '18 22:02 js-choi

There's a few choices that use ? but might avoid some of the confusion:

  • <?>
  • {?}

Also, I find ^^ confusing in that what it does is not related to what ^ does.

ljharb avatar Feb 04 '18 22:02 ljharb

FWIW, I really like ^^. I find it clever, and illustrative in that it points up, as though at the previous expression which is likely to be the LHS of the pipeline operator, reminding you where the value comes from.

aikeru avatar Feb 04 '18 22:02 aikeru

Pipelines will be single-line just as often as multiline; so "points up" and "points at the previous" won't consistently be the same.

ljharb avatar Feb 04 '18 22:02 ljharb

I take it that, if we go down the Hack-style path, we're not trying for any kind of path for the placeholder to be subsumed by @rbuckton's partial application proposal, and therefore should avoid using the same token, is that right?

littledan avatar Feb 04 '18 23:02 littledan

My 2c: I personally really like $ as the placeholder variable binding. Because it's a valid identifier, it feels like an actual variable, which it certainly is (albiet smaller in scope). It also prevents the Hack-style proposal from claiming another operator that might be useful for future proposals.

I think the "jQuery problem" is probably overblown for these reasons:

  • How many libraries out there actually expose a global $? I would imagine very few.
  • The pipeline operator does not impede using these libraries. Rather, using these libraries impedes using the pipeline operator, which is not a big deal.
  • jQuery would hardly benefit from the pipeline operator anyway since it uses heavy method chaining.
  • If you're not using global scripts and using modules instead, then you can easily rename your import / require variable to something other than $.
  • _ would still available, and is much more commonly used for utility functions (lodash, underscore, rambda, etc.), although becoming less so due to static ES module imports.

gilbert avatar Feb 04 '18 23:02 gilbert

Is it at all possible to use $ in the partial application proposal? If we're trying to leave open the door for a partial application proposal in the future, we could end up with two different syntaxes for partial application / placeholders, one inside pipelines and one outside.

mAAdhaTTah avatar Feb 05 '18 00:02 mAAdhaTTah

@mAAdhaTTah I don't think it's possible for partial application to use $ unless there's a token which is used to start off the partial application expression. The current partial application proposal does not have such a token.

littledan avatar Feb 05 '18 00:02 littledan

@littledan: I take it that, if we go down the Hack-style path, we're not trying for any kind of path for the placeholder to be subsumed by @rbuckton's partial application proposal, and therefore should avoid using the same token, is that right?

From what I could tell, syntactic partial application could use the same placeholder as Hack style iff syntactic partial application were forbidden in every placeholder-using pipe’s RHS. This could be a footgun—it creates a hidden context dependency between two very different results, which may cause the programmer to accidentally commit a mode error—but if we logically proceed anyway:

Proposal 2: Hack Style Only and Proposal 4: Smart Mix would forbid syntactic partial application in the RHS of all pipes—or, rather, the parser would always interpret the presence of a RHS placeholder as a pipe placeholder, not a partial-application placeholder. For instance, if |> is always a Hack pipe and ? is the placeholder for both |> and partial application, then f(?, 3) would be partial application, and x |> f(?, 3) would be f(x, 3).

Proposal 3: Split Mix would forbid syntactic partial application in the RHS of Hack pipes. It could also forbid it in the RHS of F-sharp pipes but it could also allow them. If |> is an F-sharp pipe, |: is a Hack pipe, and ? is the placeholder for both |: and partial application, then f(?, 3) would be partial application, x |> f(?, 3) would be f(?, 3)(x) (or it could be a syntax error), and x |: f(?, 3) would be f(x, 3).

If that analysis is correct, I’ll add it to the original post’s ? option. The problems of hidden contextual dependency—and of visual confusion with nullish coalescing and optional method chaining (assuming that syntactic partial application would stick with ?)—would remain.

js-choi avatar Feb 05 '18 05:02 js-choi

I would be very opposed to using any valid identifier - including $. It is a very common pattern to do var $ = document.querySelectorAll as well, for example - it’s not just about globals.

Shadowing bindings is fine when users explicitly choose the identifier - it would not be ok with me that a magic implicit binding would appear that shadows my code just for using an operator.

ljharb avatar Feb 05 '18 05:02 ljharb

I prefer personally ?, but I think it's worth unpacking 'visually confusing'. I think the statement that it is visually confusing in relation to the two other stage 1 proposals is misleading — those statements are more visually confusing with each other and with the ternary operator than anything else. Even if both of those get through, the ? placeholder proposal should not be held back on their account.

The ambiguity only creeps in when placeholders are nested in further expressions — @gilbert's original example being a placeholder in a ternary expression: ? ? : foo. This is the kind of scenario where the other two stage 1 operator proposals become problematic, when ?? can be read as either an optional chain on the placeholder.

It's a truism that nested ASCII operations are difficult to parse, but I think the placeholder is a special case inasmuch as it already bears the application-space cognitive weight of a deferred value within a special lexical scope of a higher order operation (the pipeline). So the situation only becomes complicated when we allow that the placeholder be used as an operand within a further expression nested inside a pipeline. Might it be worth outlawing that particular condition? — that is to say, a placeholder must be standalone, invocable only with ( or , either side of it?

In passing, I am firmly against the idea that an otherwise valid generic reference (like _ or $) be special cased. There is a special kind of semantic ambiguity introduced in scenarios like that of this in class arrow methods, when lexical parsing rules change based on higher order context. To do that with something which is otherwise up to user semantics is by all accounts a really imposing proposal (I foresee eslint rules to try and retro-ban them to compensate for this, invalidating previously legitimate and self-explanatory code).

barneycarroll avatar Feb 05 '18 09:02 barneycarroll

What about parameters with a name ? for example:

anArray
  |array> pickEveryN(array, 2)
  |array> array.filter(...)
  |filteredArray> makeQuery(filteredArray)
  |query> await readDB(query, config)
...

Then one could write something like this:

anArray
  |$> pickEveryN($, 2)
  |$> $.filter(...)
  |$> makeQuery($)
  |$> await readDB($, config)
...

I think this would improve code readability, as there may be a lot of transformations applied. If I want to read one particular line, I won't know what's passed to the function and what's for.

Other proposal, reuse arrow functions : [edit: actually, I noticed this is proposal 1]

anArray
  |> array => pickEveryN(array, 2)
  |> array => array.filter(...)
  |> filtered => makeQuery(filtered)
...

I like this proposal because it can be used with destructuring and stuff like this, while being readable.

There are probably a lot of issues with those proposals, but my main idea is that variables like "$" or "^^" lack of readability and are not really elegant (from my point of view).

stephaneraynaud avatar Feb 05 '18 14:02 stephaneraynaud

Whichever token we choose is going have an effect on the placeholder syntax, either by producing two different placeholder syntaxes (which I'd prefer to avoid), by superseding the placeholder syntax (effectively killing it), or by forcing its hand (requiring it to use the same token). We should probably decide which approach we're taking and keep that in mind as we bikeshed the token for use in the pipeline.


@bisouduperou I think we ran into problems with a similar proposal for await, e.g. this:

x |$> pickEveryN($, 2)

could be parsed as:

x | ($ > pickEveryN($, 2))

mAAdhaTTah avatar Feb 05 '18 15:02 mAAdhaTTah

Reusing arrow functions’ arrow works very well for most purposes, but not for await.

charmander avatar Feb 05 '18 21:02 charmander

Honestly I just really hope any form that has a placeholder variable of any shape or spelling gets adopted, because it adds tremendous power and flexibility to an operator that would otherwise be much more limited and constrained... Thanks for letting me add my 2c.

aikeru avatar Feb 05 '18 23:02 aikeru

In https://github.com/tc39/proposal-pipeline-operator/issues/84#issuecomment-363454642 I used a ## nullary operator as a pipe placeholder. It looks nice and it doesn’t mess up GitHub’s current JavaScript syntax highlighting, but it probably would be quite visually confusing with private fields, so I don’t actually think it’s a good idea, heh.

js-choi avatar Feb 06 '18 15:02 js-choi

Ok, if $ is too commonly used in user-land, then how about $$, like the actual Hack pipe operator?

gilbert avatar Feb 07 '18 15:02 gilbert

@gilbert I was also thinking about $_ as another possibility.

mAAdhaTTah avatar Feb 07 '18 15:02 mAAdhaTTah

Let's try to not end up with smileys please $_$

stephaneraynaud avatar Feb 07 '18 16:02 stephaneraynaud

Ah, $_ brings back dormant memories of Perl…

For what it’s worth, Perl 6 actually turns Perl 5’s $_ into an impressively unified concept of a lexical “topic variable”, which may be lexically bound using a given block or a for block and which is implicitly used by many other types of statements, operators, and functions, making for rather pithy tacit code. Think of it like linguistic topics. And in general, Perl 6’s design (fun fact: they actually released 6.0 some time back) is worth checking out you want to dive into the heart of some ingeniously mad and madly ingenious PL design.

I am in no way suggesting that an implicit topic variable be a thing in JavaScript beyond Pipe Proposal 2/3/4, though it’s pretty fun to think about.

js-choi avatar Feb 07 '18 16:02 js-choi

@gilbert tbqh, i very strongly would object to any valid identifier there - it’s not just about “commonly used” for me, it’s about “ever used”.

ljharb avatar Feb 07 '18 16:02 ljharb

@ljharb I think such a strong stance to take would make sense if $$ were made globally available, but here it's only on the right-hand side of a pipeline – a very small scope, of which a programmer consciously introduces by writing |> in the first place.

gilbert avatar Feb 07 '18 18:02 gilbert

Magically introducing an implicit binding that shadows something defined in an outer scope is with-like behavior, and after we all worked so hard to remove with from strict mode, I’d think we’d be loath to reintroduce it.

ljharb avatar Feb 07 '18 18:02 ljharb

I agree it's implicit, but I would not equate to with. Yes we'd be loath to reintroduce with, but this is not remotely the same thing.

gilbert avatar Feb 07 '18 18:02 gilbert

PowerShell also uses $_ as a topic variable in a number of places:

// pipes
Get-Files | ForEach-Object { Out-Host $_ };

// catch blocks
try { ... }
catch [System.Exception] {
  Out-Host $_.Exception.GetType().FullName; 
}

// filters/functions
function Foo {
  begin { ... }
  process { Out-Host $_; }
  end { ... }
}

Of course, that's because every variable in PowerShell is prefixed with $...

rbuckton avatar Feb 07 '18 21:02 rbuckton

Ah, $_ brings back dormant memories of Perl…

PowerShell also uses $_ as a topic variable in a number of places:

FWIW, Ruby uses this too. If JS were to adopt $_ as a topic variable or something like it, there's lots of precedent.

aikeru avatar Feb 08 '18 02:02 aikeru

there's lots of precedent.

Node's also set precedent with _ being the last evaluated expression, but if it's defined in-scope it the variable you set it to.

jridgewell avatar Feb 08 '18 03:02 jridgewell

There is also Safari Web Inspector’s use of $1, $2, … to denote previously evaluated expressions in its console REPL, I suppose.

I used to be very against using a valid identifier for a pipe placeholder, but being reminded of Perl and Ruby made me realize that it might not be that weird. Then again, that there is an implicit binding at all in a Proposal-2,3,4 pipe is relatively unprecedented in ES. with was very bad especially because of its mucking up of static optimization, which Proposal-2,3,4 pipes do not have at all. But could using a valid identifier make static analysis of a pipe’s RHS—e.g., to determine whether there is no placeholder in the RHS—more difficult?

I plan to have my Babel plugin for Proposal 4 support configuration of what its placeholder is, to encourage experimentation (if its tokenization permits it). What its default placeholder should be, I don’t yet know. Maybe a non-ASCII placeholder by default might be interesting after all, haha.

js-choi avatar Feb 08 '18 04:02 js-choi

I don't think it's about weirdness but about origin. That you don't know about what an operator means is ok. You may don't know what "yield" or "await" means, because those are operators. But a valid identifier is, by default, a variable previously defined somewhere. By adding valid identifiers like "$_", you will need to ask yourself "am I in pipe ?" "does it come from pipe" "is it a global / simple variable?". I deeply encourage to avoid such (little) confusion.

While I personnally prefer meaningful placeholders ("$_" is as readable as "let thing = stuff()"), why not using @ as placeholder?

anArray
  |> pickEveryN(@, 2)
  |> @.filter(...)
  |> makeQuery(@)
  |> await readDB(@, config)
...

stephaneraynaud avatar Feb 08 '18 08:02 stephaneraynaud

@bisouduperou: I agree that too much context sensitivity is undesirable. I am not a big fan of shadowing $; I might want to use an outer $ in a pipeline’s RHS, although I suppose that I could always just reassign it to another variable. I’m gradually warming up to $ vis-à-vis Perl/Ruby, although at least one TC39 member has expressed strong disapproval of $ for reasons with which I agree.

In any case, @ is probably impossible as a pipe placeholder due to its use as a prefix by the stage-2 decorators proposal.

@rbuckton wrote a useful list of current operator uses in https://github.com/tc39/proposal-partial-application/issues/21#issuecomment-361092565. There are some ASCII characters here that have not yet been considered for pipe placeholders. ~~ particularly comes to mind as an option, although your mileage may vary with the flavor of ASCII soup it might bring. In any case, I’ll try to add ~~ to the original post and the wiki later today.

js-choi avatar Feb 08 '18 12:02 js-choi

In light of some possible confusion that has occurred around the phrase “Hack style” (Proposal 2 only versus Proposals 2,3,4), I’m renaming this issue. I may do the same in the wiki, hopefully in a way that doesn’t break previous anchor links.


There’s been quite a bit of discussion in #89 on the possible interactions between Proposals 2,3,4 and other proposals, particularly @rbuckton’s syntactic partial application (PA) and HOF operators. I’ll try to update the issue original post and wiki later today with points from the discussion.


[@littledan, #93 (comment)] I'd prefer if we can minimize the number of tokens we introduce to make the feature set easier to learn. From that perspective, the optimal solution would be one token for pipeline, and one token for partial application and/or pipeline placeholder.

[@zenparsing, #89 (comment 1), #89 (comment 2)]: Yep, partial application is a red herring with respect to pipelines.…I think [this quote] expressed the idea better than I could have:

Trying to explain pipelines (which are fundamentally unary) in terms of partial application (which are n-ary) seems to me to be a fundamental mismatch in their models.

Putting these here from other issues because they’re TC39-member opinions relevant to whether the pipe placeholder should have the same token as syntactic PA.


As far as priorities go, we should talk about whether readability is more important to optimize than writability, particularly now that ASCII operators have largely been exhausted. “Most code will be written once then read many times,” after all. In particular, non-ASCII Unicode syntax characters may be useful. A reserved keyword might also be worth considering, although perhaps not.

Let’s take a stark example of the writability/readability: a non-ASCII syntax character. may be more visually readable than the ASCII soup ~~, but the latter is far easier to input. optimizes for readability at the expense of writability; ~~ optimizes for writability perhaps at the expense of readability. To what extent the trade off is worth one way or the other is difficult to ascertain.

For what it’s worth, Perl 6, Julia, and Scala support and idiomatically use non-ASCII Unicode operators (there are also some amusing responses in a Reddit thread from 2016).

Of course, there is infamously also APL, which required encoding of numerous special operators in Unicode; its successor, the J language, eschews those non-ASCII operators in favor of ASCII digraphs—whether the resulting soup is readable enough to be worth it is up to you.

A long conversation over non-ASCII operators’ trade offs in Julia occurred in 2012. Julia’s REPL and IDE offer text “expansion” (contraction, rather) of LaTeX-style backspace-delimited abbreviations for non-ASCII characters, such as \dot for ·.

Perl 6 tries to accommodate both readability and writability by offering both non-ASCII Unicode operators and ASCII equivalents of the same operators, e.g., vs. !=: TIMTOADY / TIMTOWTDIBSCINABTE versus TOOWTDI. Perl 6 also offers advice on actually inputting Unicode characters in general.

There is also the precedent of FiraCode by @tonsky, a monospace font with numerous ligatures for improved visual display of many common operator spellings, and which sidesteps the writability tradeoff completely, in exchange for a one-time installation cost. Relatedly, the same author offers a clojure-unicode library for operators like λ, , etc., instead of fn, not=, ->, etc.—although in Clojure, being a Lisp, operators are literally the same as variable symbols.

Another aspect of non-ASCII is editor/IDE support for display, but it is already a fait accompli that ES supports non-ASCII variable names and Unicode characters in general. JavaScript’s application is the web, after all, as well as Node.js, both of which are already strongly coupled to Unicode.

Of course, all is this is moot if it is decided that $ is okay after all, although at least one TC39 member has above expressed strong disapproval of $.


In any case, I’ll try to update the issue’s and wiki’s list of possible placeholders sometime today with ~~, $_, $_$ (kidding), the from #89 for illustrative purposes, the elsewhere-suggested (?), and ? shared with PA. And whatever reserved keywords are available, I guess. private, anyone? I’m joking.

js-choi avatar Feb 08 '18 13:02 js-choi