Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Tables and Records

January 14, 2010

Many languages have an associative collection data type built into a language, for example Python has a dictionary, Lua has tables and JavaScript has objects. I want something similar in Heron,  but I will be putting my own spin on it.

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();

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?

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.