Skip to content

catch syntax is confusing (looks like an annotation) #2948

Closed

Description

@DartBot

This issue was originally filed by sammcca...@google.com


Types are both used as annotations (don't affect behaviour) and as references (do affect behaviour), in different contexts:

Annotations:
  T local = 42;
  T method() { }
  method(T param);

References:
  new T();
  x is T
  List<T>
  extends T
  catch (T foo)

Note that catch syntax fits the pattern of an annotation, but it's actually a behaviour-affecting reference to the type!
This confuses people (myself, and others on the mailing list).

The catch syntax originates with languages that don't have this inconsistency, because types are enforced rather than annotations. In these languages, the most similar syntactic construct (method parameter declarations) is used for a pattern matching construct (overloading) similar to catch. This similarity is not present in Dart, so the similar syntax is misleading.

I think the following syntax is an improvement:

  } catch (x is T) {

Pros:
  * syntax is already reserved
  * type appears at the end of the expression, like other type references
  * meaning is obvious
Cons:
  * looks like a boolean expression
  * less familiar than current syntax to Java/C++/C# developers

There's probably even better syntax to be found, though.

Activity

  1. anders-sandholm commented on May 9, 2012

    @anders-sandholm

    Removed Type-Defect label.
    Added Type-Enhancement, Area-Language, Triaged labels.

  2. gbracha commented on May 9, 2012

    @gbracha
    Contributor

    I agree the use of types in the catch syntax is at odds with the rest of the language.


    Set owner to @gbracha.
    Added Accepted label.

  3. gbracha commented on May 29, 2012

    @gbracha
    Contributor

    Added this to the M1 milestone.

  4. gbracha commented on Jun 19, 2012

    @gbracha
    Contributor

    The next rev of the spec (0.11) updates the catch syntax to resolve this issue and make it cleaner.


    Added Done label.

  5. gbracha commented on Jun 20, 2012

    @gbracha
    Contributor

    Analyzer issue #3576
    VM issue #3577
    dart2js issue #3578

  6. sethladd commented on Jun 20, 2012

    @sethladd
    Contributor
  7. DartBot commented on Aug 8, 2012

    @DartBot
    Author

    This comment was originally written by @mhausner


    Gilad, the spec does not say whether "on" is a reserved word or a built-in identifier. Please update the spec accordingly.


    Added NeedsInfo label.

  8. gbracha commented on Aug 8, 2012

    @gbracha
    Contributor

    That is because it is neither. The word "on" was deemed too important in web APIs etc.

  9. DartBot commented on Aug 14, 2012

    @DartBot
    Author

    This comment was originally written by @aam


    Correcting links to relevant issues:
      Analyzer http://code.google.com/p/dart/issues/detail?id=3756
      VM http://code.google.com/p/dart/issues/detail?id=3757
      dart2js http://code.google.com/p/dart/issues/detail?id=3758

  10. gbracha commented on Aug 14, 2012

    @gbracha
    Contributor

    Added Done label.

  11. added this to the M1 milestone on Aug 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions