Ticket #252 (new defect)

Opened 1 year ago

Last modified 1 year ago

Syntax: Reformed with and switch type annotation after head looks weird

Reported by: brendan Assigned to: jeffdyer
Type: defect Priority: major
Milestone: Component: RefImpl
Version: 4 Keywords:
Cc: lth, dherman, brendan, waldemar@google.com, cormac, graydon

Description

See the WP:

let o = new Point(1, 2) // Point has x and y fixtures
let x = 10, y = 20
with (o): {x: int}
    print(x + y)

Ouch -- I was party to this change from putting the annotation inside the parenthesized head of the switch type or with. The rationale IIRC was that otherwise, you could see, e.g.

with ({p:42, q:true} : {p:int, q:boolean} : {p:int}) ...

But really, who cares? Corner cases such as this don't justify the odd-looking annotation that comes after the head closes with a right parenthesis.

What's more, we could forbid anything like the above as written (you could still overparenthesize the annotated object initialiser), as the final annotation trumps the one on the initialiser for the purposes of the with or switch type (I think).

Comments welcome, this is a small syntax fix at most, so fits in a trac ticket.

/be

Attachments

Change History

Changed 1 year ago by lth

I'm in favor of placing the annotation (back) inside the parens.

I think the rule should be that ObjectLiteral : TypeExpr is always interpreted as annotating the literal with a type, even in the context of "with" and "switch type". Associates left-to-right. If there is a second constraint as allowed by those two statements then it must follow a second colon.

I don't think there's any need for prohibiting anything (though if pressed I would probably argue for prohibiting the case where only one type expression is seen, since that's the one that's ambiguous.)

Changed 1 year ago by lth

  • component changed from Proposals to RefImpl
  • summary changed from reformed with and switch type annotation after head looks weird to Syntax: Reformed with and switch type annotation after head looks weird

By agreement among Jeff, Brendan, Lars.

Changed 1 year ago by brendan

As I think Lars pointed out in person, we can't say the case of

with ({p:42} : {p:int}) ...

is a case of reformed with, since there are no guarantees on p's presence or type, and the type annotation by itself should not imply wrap.

So yeah, we require two colons and two types for this corner case, which is not likely to arise with with anyway.

/be

Changed 1 year ago by waldemar

I don't like the colon after the closing parenthesis either. I suspect it wouldn't be too hard to modify the expression grammar of the with-expression to disallow a colon at the top level, thus permitting a colon there to acquire a special meaning.

Note: See TracTickets for help on using tickets.