Swift style syntax
Isiah Meadows
isiahmeadows at gmail.com
Tue Oct 13 06:06:58 UTC 2015
+1 for operators as functions (I frequently is them in languages that have
them), but there is an ambiguous case that frequently gets me: does `(-)`
represent subtraction or negation. It's usually the former in languages
with operators as functions.
But here's a couple other potential syntactical ambiguities, dealing with
ASI:
```js
// Is this `x => f(x)` or `x = (>); f(x)`
x =>
f(x)
// Is this `-x` or `-; x`?
-
x
```
Those can be addressed with a cover production to be used for expression
statements and direct value assignment, requiring parentheses to clarify
the latter case in each.
A similar ambiguity problem, arguably harder to resolve, is partially
applied subtraction, such as `(- 2)`. Is that a -2 or is it equivalent to
`x => x - 2`? I will caution on this idea, as I know that's the next
logical step.
On Mon, Oct 12, 2015, 06:43 Thomas <thomasjamesfoster at bigpond.com> wrote:
>
> Is it possible to extend JavaScript syntax to support Swift style block
> syntax[1]?
>
> In Swift it's possible to omit return keyword
> ```
>
> 1. reversed = names.sort( { s1, s2 in s1 > s2 } )
>
> ```
>
>
> As you note below this is already possible in es6, and might I add, has
> much more intuitive syntax in Es6. The swift syntax looks like a list
> comprehension gone wrong.
>
> or omit argument declaration like this:
>
> ```
>
> 1. reversed = names.sort( { $0 > $1 } )
>
> ```
>
>
> I for one think this is a bad idea - use rest arguments instead. It's
> pretty terrible as far as readability goes, although I'd like to see more
> examples of it being used in Swift code.
>
> or apply an operator to arguments of a function
>
> ```
>
> 1. reversed = names.sort(>)
>
> ```
>
>
> This might actually be possible - I can't think of any ambiguous
> situations for passing operators as if they were first class functions. If
> it is possible, I'd like to see this done.
>
> We have the first feature in ES2015 already:
>
> ```
> let sorted = names.sort((a, b)=> a > b);
> ```
>
> But for omitting argument declaration we need to find an alternative to
> $0, $1... since those are valid variable names in JS. Maybe we can use #0,
> #1... instead.
>
> This is very useful for functional programming aspect of JS. For example
> in a filter function:
>
> ```
> let passed = objs.filter(#0.passed)
> ```
>
> [1][
> https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
> ]
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151013/cd286666/attachment-0001.html>
More information about the es-discuss
mailing list