proposal-pipeline-operator
proposal-pipeline-operator copied to clipboard
Placeholder name, eg %name
I wanted to propose adding an optional placeholder name to the syntax and ask you what do you think about it?
Example:
const fileName = "file.txt";
fileName
|> addServerPath(%) // `/path/to/server/file.txt`
|> convertToUrl(%filePath) // `http://server.com/path/to/file.txt`
|> await fetchFile(%fileUrl) // some node file object
|> await saveFile(%fileHandler, "downloads")
As seen, in the pipe steps we add the name of the placeholder. It is optional and has 0 impact on how this code works, but makes it easier to read, as we can add the information 'what is the placeholder at this step' so the reader of the code doesn't have to guess it from function names.
Don't comments work for that purpose? I often use them to document what each parameter is when calling a function, regardless of pipes.
const fileName = "file.txt";
fileName
|> addServerPath(%) // `/path/to/server/file.txt`
|> convertToUrl(/* filePath */ %) // `http://server.com/path/to/file.txt`
|> await fetchFile(/* fileUrl */ %) // some node file object
|> await saveFile(/* fileHandler */ %, "downloads")
Of course this can be addressed with comments, but placeholder could be a nice "baked in" way to do that.
A bit similar to why we give variables good names if possible before writing a comment.
This probably was discussed in #91.
A bit similar to why we give variables good names if possible before writing a comment.
If you have good function names, you may not need to give variables names at all.
A bit similar to why we give variables good names if possible before writing a comment.
If you have good function names, you may not need to give variables names at all.
True, but also quite subjective. What do you feel is the reason against placeholder names? In the end you don't have to use them if you don't want to
I don't have any reason against placeholder name, because I don't oppose it. It may not be needed but I would say that it is not a bad thing either.
Isn't avoiding giving the name 90% of the choice to use |>
though? This would make parsing harder (and I assume slower), and would be one more thing to teach. I have a feeling like a simple comment is more useful here, even a "normal" one at the end of the line, like you'd do it now when chaining things like map
and filter
.
Also, IDEs are very likely to give an inline hint for the argument name right after %
anyway, so it will pretty much look exactly like OP's example in practice.