Ticket #139 (new defect)

Opened 1 year ago

Last modified 1 year ago

(Resolved) Syntax: require braces in method body for constructor

Reported by: lth Assigned to: jeffdyer
Type: defect Priority: major
Milestone: M2 Component: RefImpl
Version: 4 Keywords:
Cc: brendan,jeffdyer

Description (last modified by lth) (diff)

This one bites me time and time again:

  class C {
    function C(a, b) a=a, b=b {}
    var a, b
  }

Spot the bug? There's a : missing after the parameter list, but the code is well-formed because the constructor can be an expression function, and here the body is "a=a, b=b" (which has no effect, certainly it does not initialize the instance variables). The pair of braces is treated by the parser as an empty instance initialization statement.

It's probably very rare indeed that the constructor will be an expression function -- constructors can't return values! So I propose that we require the braces around its body, so that this probably fairly common error will be reliably detected.

Attachments

Change History

follow-up: ↓ 2   Changed 1 year ago by lth

  • description changed from This one bites me time and time again: class C { function C(a, b) a=a, b=b {} var a, b } Spot the bug? There's a `:` missing after the parameter list, but the code is well-formed because the constructor can be an expression function, and here the body is "a=a, b=b" (which has no effect, certainly it does not initialize the instance variables). The pair of braces is treated by the parser as an empty instance initialization statement. It's probably very rare indeed that the constructor will be an expression function -- constructors can't return values! So I propose that we require the braces around its body, so that this probably fairly common error will be reliably detected. to This one bites me time and time again: {{{ class C { function C(a, b) a=a, b=b {} var a, b } }}} Spot the bug? There's a `:` missing after the parameter list, but the code is well-formed because the constructor can be an expression function, and here the body is "a=a, b=b" (which has no effect, certainly it does not initialize the instance variables). The pair of braces is treated by the parser as an empty instance initialization statement. It's probably very rare indeed that the constructor will be an expression function -- constructors can't return values! So I propose that we require the braces around its body, so that this probably fairly common error will be reliably detected.

in reply to: ↑ 1   Changed 1 year ago by jeffdyer

  • owner set to jeffdyer
  • component changed from Proposals to Spec
  • milestone set to M2

Replying to lth:

Indeed this is a semantic error that is not being caught. I agree with Lars that we should make it a syntax error to have a constructor with a brace-less body.

Assigning to me.

  Changed 1 year ago by lth

  • component changed from Spec to RefImpl
  • summary changed from Syntax: require braces in method body for constructor to (Resolved) Syntax: require braces in method body for constructor
Note: See TracTickets for help on using tickets.