Ticket #370 (new defect)

Opened 9 months ago

Last modified 9 months ago

Nominal type annotation on array and object literals

Reported by: lth Assigned to: anonymous
Type: defect Priority: major
Milestone: Component: Spec
Version: 4 Keywords:
Cc: graydon, brendan, jeffdyer, etierney, edwsmith

Description

We've agreed in the past that it should be possible to annotate array and object literals with array and object structural types, respectively.

The Clique of Three decided today that this should be extended to nominal types.

If T is a nominal type of any kind then

  [x,y,z] : T

means exactly

  let (tmp = new T(3))
    ( tmp[0] = x,
      tmp[1] = y,
      tmp[2] = z,
      tmp )

and

   { x: v1, y: v2 } : T

means exactly

   let (tmp = new T)
     ( tmp.x = v1,
       tmp.y = v2,
       tmp )

The killer use case here is that you want array literals to be annotated by a Vector type:

   [ 1, 2, 3 ] : Vector.<int>

but it generalizes well, so we think the generalization should be allowed.

Attachments

Change History

Changed 9 months ago by lth

BTW the symmetry with how structural types have evolved is interesting. In the case of nominal types, we've gone from

   new T(...)

to also accomodate the style

   ... : T

whereas in the case of structural types we've gone from the primitive form

   ... : T

to also allow (though not always without controversy)

   new T( ... {

Changed 9 months ago by lth

  • cc changed from graydon,brendan,jeffdyer to graydon, brendan, jeffdyer, etierney, edwsmith

Another useful observation is that Vector has been designed as a drop-in replacement for most casual uses of Array, but the lone holdout was that literal syntax would have to be changed to constructor call syntax. This ticket fixes that problem.

(Now the only remaining problem is that a lot of people will be annoyed about having to write the type twice if they use an annotated LHS. Given the new syntax we *could* fix that by Java-style inference of the RHS type from the LHS type: If we're in a binding form and the LHS is typed and the RHS is a literal or an object/array initializer, then the :T is slapped on the RHS automagically. Discuss. We could prototype in ActionScript?...)

Note: See TracTickets for help on using tickets.