Tables and Records
Here is how I am currently planning on adding support for primitive associative collections in the next version of Heron.
I originally wasn't going to build support for associative collections directly into the language, but it often turns out to be very convenient to have syntax for supporting their usage. After using languages with support for writing [apple="red", orange="orange", banana="yellow"] using languages without support for it feel primitive and lacking.
The other reason for language support, rather than just library support, is performance. If these are built into the language, there is a chance for the compiler to do more powerful optimizations.
My current plan for primitive associative collections in Heron would involve introducing two separate types called tables and records.
The record would a fixed length associative array, that can be indexed by name or index. It is like a dictionary, but once constructed doesn't allow more values to be added. Each member of a record is a key/value pair. The value could be any type, but a key would be a string. You would declare records as so:
var r : Record = [index=0, type="fruit", color="red"];
This looks a lot like an object initializing syntax, doesn't it?
So how does this vary from a class instance? Well the there is no name associated with the record layout, so it behaves like an anonymous type. Also it supports indexing by run-time strings as so:
var s = Console.ReadLine(); Console.WriteLine(r[s]);
Unlike many languages I am going to explicitly disallow numerical indexing of fields. This will make it easier for the compiler to optimize code, because the placement of fields will not be predetermined.
Of course, not being able to add new key/value pairs is of limited use. A separate construct (table) would introduce this functionality.
The table would be an unordered sequence of records each with the same layout that supports insertion and deletion. A table constructor would like this:
var t : Table = [fruit:String, color:String]; t.Add([fruit="apple", color="red"]); t.Add([fruit="banana", color="yellow"]);
I would appreciate any feedback on this feature. What do you think of the syntax, semantics, and names? Is this intuitive or confusing? Do you think it would be useful?