-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Too much question mark #21
Comments
Maybe this silly example can help // partial + ternary operator + optional chaining + nullish coalescing in the same call.
const doStuff = (x, y, z, w) => {
// do something with x, y, z and w
}
const curriedDoStuff = doStuff(?, v.?a ?? 0, v.b > 10 ? 10 : 0, ?); And I think we can easily be more cryptic. |
Just to name an alternative: how about the dot, like in R? const curriedDoStuff = doStuff(., v.?a ?? 0, v.b > 10 ? 10 : 0, .)
const curriedDoStuff = doStuff(*, v.?a ?? 0, v > 10*b ? 10 : 0, *) but the |
How about something like a string format? It would allow you to specify which argument you want: const curriedDoStuff = doStuff(%, v.?a ?? 0, v > 10*b ? 10 : 0, %...)
const curriedDoStuff = doStuff(%0, v.?a ?? 0, v > 10*b ? 10 : 0, %1)
const curriedDoStuff = doStuff({0}, v.?a ?? 0, v > 10*b ? 10 : 0, {...})
const curriedDoStuff = doStuff({0}, v.?a ?? 0, v > 10*b ? 10 : 0, {1}) |
I really, really wish that the underscore character was available! |
Since it cannot be used as a variable character, I propose using the arobase character @ for place-holder and @@ for rest placeholder. const f = (x,y,z,w) => ( (x * y) - ( z *w) )
// "ordered placeholders"
const g = f(@, 12, @, 14)
// then g = ( a1, a2 ) => f( a1, 12, a2, 14)
g(11, 13) === f(11, 12, 13, 14)
// "rest placeholder"
const h = f( @@, 14)
h(11, 12, 13) === f(11, 12, 13, 14)
// "numbered placeholders"
const k = f( @3, @1, 13, @2 )
// equivalent to
const k = ( a1 , a2, a3) => f( a3, a1, 13, a2)
k(21, 22, 23) === f( 23, 21, 13, 22 )
// even
lodash_filter( @2, @1) == ramda_filter
lodash_filter == ramda_filter(@2,@1) Moreover, @ naturally sounds like "arg" (ok pittyful argument :P) |
Moreover, |
This would be ambiguous with future support for decorators on arrow functions and function expressions. |
Well, the question mark is already in use and for 90% of time it's used to deal with null values ( So I propose finally:
Finally, all of this could be short synthax for decorators, for example: @partial(), @partial(1), @partial(...) |
Interesting single-character tokens on a standard keyboard already have a use in JavaScript, unfortunately:
Anything that is a prefix token is out due to forwards incompatibility with possible future features such as positional placeholders. Balanced tokens (such as Of the remaining three characters, As to ambiguity with other proposals, the
The optional chaining proposal is one reason why I did not allow As with the optional chaining proposal, the coalesce proposal is considering changing their token to As far as "too many question marks" that all depends on coding style. You can already have perfectly legal javascript with ar |> map(?, x => x > 0 ? true : false);
ar |> map(?, x => x ?? "missing"); As such, I believe that |
Also, regarding using I have considered eventually proposing an "operator function" shorthand syntax to pair with pipeline and partial application as something like I haven't put this proposal before the committee yet as that would be far too much syntax to propose all at once. |
Ok..... as I understand it, the @ is for decorators, but a decorator has the form
I admit I didn't understand this sentence
and I agree that the question mark is the best token if we cannot do anything against it. |
I'm worried that using |
@rbuckton I appreciate this thorough analysis. Have you considered using multiple character tokens? |
or even |
@littledan Other than for @ljharb I would be concerned that this would be confusing if I ever attempted to introduce shorthand operator functions. I considered
|
@ljharb I gotta say it would improve the readability a bunch but it seems over the top to me, it should be simpler.
|
I think the contrived example in the OP would look just as confusing with any other syntax. IMO |
Unfortunately |
(As an aside, |
Semantically, |
@mlanza Semantically? Please, do not mix-up with personal preferences. |
@ysaskia - I know that But Even |
To be honest, I kind of like the original proposal to use |
|
Of course, and I understand that. I only meant to point out that, even skimming the example snippets here, I often have to pause and look a bit closer to distinguish the From this perspective, I find |
How important is it to not use valid existing syntax? Technically adding Most ESNext users likely don't import |
@rajington they weren't breaking changes because it still works. |
Couldn't |
They could set the variable. Using the variable is the problem. Let's say we have function getLodashVersion(lodash) {
return lodash.VERSION;
}
console.log(getLodashVersion(_)) where console.log((_placeholder) => getLodashVersion(_placeholder)) i.e. it prints a new function instead of the result of calling the original function. |
|
Ignoring the other proposals, what would be the expected behaviour if I used the ternary to conditionally apply the argument placeholder? i.e.
I mean, it'd have to work right? I can't think of other examples where using the ternary operator to conditionally pass an argument would be disallowed. It's really ugly though if ? is the truthy case.
I like the question mark above all else if |
It wouldn't work: |
Here's a similar example with ES6 syntax:
|
I am biased towards using
|
What about
The binding proposal that was gonna use it doesn't seem to go anywhere. |
10::add(1)::mul(2) |
There is another option to use 10 |> add(1, \0) |> mul(\0, 2)
var work= (key, value)=>{...}
[...].map(work(\1, \0)); not as good as |
The best argument against I really don't see any reason the parser would have a problem using a special character with a prefix usage. So long as
|
@rbuckton since #37 was redirected here, I wanted to ask about the last comment you made there re
If we used e.g. foo
|> add(@0, 2)
|> fetchData(@0)
|> await
|> @.json()
|> await As mentioned, there's no conflict with decorators there and it's pretty intuitive. Are there other use-cases that would be needed in future where Sorry if this has been covered in the other issues, but I couldn't find anything specific about what the problem would be with |
It also looks like lambda, and used for this purpose in Haskell for example. What concerns me even more than abusing the poor array.map(clamp(-50, 100, ?)); to run into confusion just by wrapping it into another function: array.map(Math.abs(clamp(-50, 100, ?))); |
I really love this proposal and I want it to be implemented but... I believe that the question mark is not a good choice. We already have the existing ternary operator and probably later we'll have the implementation of the 2 proposals optional chaining and nullish coalescing. Obviously, the futur js code will looks like very ugly hieroglyphs.
Why not an asterisk or another special character ?
It's just my personal opinion but maybe it's just me.
Too much question marks...
The text was updated successfully, but these errors were encountered: