jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.49k stars 1.99k forks source link

deprecate "::" #1678

Closed showell closed 13 years ago

showell commented 13 years ago

I expect this to be closed quickly, but it might be useful to have on the record. I think "::" should be deprecated.

  1. Its semantics are strange for JS users, because it neither maps to an operator nor a keyword.
  2. You can write nontrivial programs without "::". For example, the CS compiler itself avoids this idiom.
  3. It pollutes the syntax (especially with ":" having its own meaning).
  4. The documentation underplays it already--it's basically a convenience for folks who can't buy into "class", which just exposes one of the worst aspects of JS, i.e. too many non-conventional ways to make classes.
  5. Last but not least, there's already a perfectly good way to say ".prototype.".

See also issue 1601

michaelficarra commented 13 years ago
  1. Not strange at all. It's a property access, just like . or [], except it's applied to the prototype of the left operand instead of the left operand itself.
  2. You can write non-trivial programs without all optional syntax. That's why it's called optional. Don't know what you mean about the compiler avoiding it:

    $ grep -r :: src
    src/nodes.coffee:        Array::push.apply ifn.body.properties, @properties
    src/nodes.coffee:# A `.` access into a property of a value, or the `::` shorthand for
    src/nodes.coffee:  filterImplicitObjects: Call::filterImplicitObjects
    src/nodes.coffee:  jumps: While::jumps
    src/lexer.coffee:      (prev = last @tokens) and (prev[0] in ['.', '?.', '::'] or
    src/lexer.coffee:LINE_CONTINUER  = /// ^ \s* (?: , | \??\.(?![.\d]) | :: ) ///
    src/lexer.coffee:CALLABLE  = ['IDENTIFIER', 'STRING', 'REGEX', ')', ']', '}', '?', '::', '@', 'THIS', 'SUPER']
    src/rewriter.coffee:        return yes if tag in ['.', '?.', '::'] and @tag(i - 1) is 'OUTDENT'
    src/grammar.coffee:    o ':: Identifier',                          -> [(new Access new Literal 'prototype'), new Access $2]
    src/grammar.coffee:    o '::',                                     -> new Access new Literal 'prototype'
    src/grammar.coffee:  ['left',      '.', '?.', '::']
    $ grep -r \\.prototype\\. src
    src/nodes.coffee:    Array.prototype.indexOf || function(item) {
    src/nodes.coffee:  hasProp: -> 'Object.prototype.hasOwnProperty'
    src/nodes.coffee:  slice  : -> 'Array.prototype.slice'
    $

    The only 3 uses of .prototype. are in JS.

  3. It's a perfectly natural syntax. A::b and A#b are often used to reference instance methods or class methods in documentation. It's natural to actually accept it as executable code.
  4. This argument is unrelated. It's an operator for accessing the prototype. The class syntax is a shorthand for defining a constructor with a bunch of things on the prototype or the constructor itself in one block.
  5. Simple macro sugar provides a convenience. It's never necessary, but this syntax just makes sense.
showell commented 13 years ago

@michaelficarra The places where the CS compiler references "::" in grammar.coffee, lexer.coffee, and nodes.coffee don't illustrate the utility of "::"; they illustrate the cost of supporting it. Likewise for the two places it shows up in the comments in nodes.coffee. All told, the compiler only uses "::" as an actual feature in two places, "Call::filterImplicitObjects" and "While::jumps".

I agree that the syntax has some convenience and learnability, but I think we differ on how high the bar should be before you introduce new syntax.

The CS compiler is over 4000 lines of code. It uses "::" twice. Using that small sample set, I'd say the syntax doesn't have much use.

showell commented 13 years ago

s/introduce new syntax/keep around syntax/

The fact that "::" already exists obviously makes it harder to eliminate it, but if it's really just a legacy bridge from JS, as I believe, then it's gonna be a wart in the future, and the earlier you eliminate it, the better.

TrevorBurnham commented 13 years ago

I think this issue is a non-starter, @showell. :: is widely regarded as one of CoffeeScript's handier sugars. Closing unless I hear a "+1" soon...

michaelficarra commented 13 years ago

@TrevorBurnham: agreed.