Ticket #165 (new defect)

Opened 3 years ago

Last modified 2 years ago

(Resolved) Simple sugar for allocation of structurally typed record data

Reported by: lth Assigned to: anonymous
Type: defect Priority: trivial
Milestone: Component: Spec
Version: 4 Keywords:
Cc: brendan,jeffdyer,cormac,dherman

Description

(Also see ticket #69, which talks about array data.)

Suppose there is a type t:

type t = { x: int, y: int, z: int }

The way to create an instance of this type now is with a structure initializer and appending the type at the end:

x = { x: 10, y: 20, z: 30 } : t

This syntax is nice because it provides a simple upgrade path for old code (just attach ": t") but not so nice because it's noisy and the type hides at the end, which is not true for nominal types, structural array types, or ES3-style function constructors.

Proposal: allow this syntax to be used:

new t(10, 20, 30)

if t is a structural record type. A new object tagged with the correct type is created and the values are assigned to its fields during initialization.

Points of argument:

  • if there are values missing, what should we do?
  • if there are too many values, what should we do?
  • if values have to be converted for the assignment to work, what should we do?

IMO we should provide defaults if possible in the first case (give'm rope), ignore supernumerary values in the second (ditto), and fail in the third (since the current language requires no conversion, except between number types).

Attachments

Change History

Changed 2 years ago by lth

  • owner deleted
  • component changed from Proposals to Spec

The construction must have the right number of arguments. If not it is a compile time error in strict mode, and a run time error in standard mode. Types must match too (this is obvious but useful to note ;-)

Changed 2 years ago by lth

  • summary changed from Simple sugar for allocation of structurally typed record data to (Resolved) Simple sugar for allocation of structurally typed record data

Changed 2 years ago by lth

  • priority changed from major to trivial
Note: See TracTickets for help on using tickets.