Swift style syntax
Isiah Meadows
isiahmeadows at gmail.com
Tue Oct 13 12:29:13 UTC 2015
I failed to mention that clearly enough... In most functional languages,
that's binary. And I think that should be the same for JS. It's surprising
otherwise.
I'd say the closest precedent here I know of would be LiveScript, where the
operator is treated as binary when used as a function. Most functional
languages have separate functions for subtraction and negation, including
Haskell (`-` vs `negate`) and OCaml (`-` and `.-` vs `negate` and
`fnegate`).
On Tue, Oct 13, 2015, 04:13 Andreas Rossberg <rossberg at google.com> wrote:
> All this is well-known from functional languages, with well-known
> solutions. The only real problem is:
>
> let f = (-)
>
> Is this unary or binary `-`?
>
> On 13 October 2015 at 08:06, Isiah Meadows <isiahmeadows at gmail.com> wrote:
> > +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
> >> ```
> >>
> >> 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:
> >>
> >> ```
> >>
> >> 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
> >>
> >> ```
> >>
> >> 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
> >
> >
> > _______________________________________________
> > 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/bdcaa15f/attachment.html>
More information about the es-discuss
mailing list