Tuples with named attributes
Nathan Allan
nathan at alphora.com
Mon Jun 18 17:05:28 PDT 2007
Has any consideration been given to the possibility of tuples with named
identified attributes, rather than ordinally identified attributes? Named
attributes are much more useful to humans, as is evidenced by the fact that
we use named rather than numbered identifiers in programming languages. In
my view, tuples, as implemented in D, would be much more useful with named
attributes.
To illustrate my point, consider what a relation (table) type might look
like in D. In the relational model of data, a relation is composed of n
tuples having the same named attributes. This allows us to define the
relation type as a set of the name : type attribute definitions. Imagine
working with a database where the attributes of each relation variable were
numbered rather than named!
Number indexed tuples are much more difficult to grok than named ones. A
name indexed tuple can be easily understood to any developers as an
"anonymous struct". Having helped design and use a language (D4) with
native support for tuples in their named form, I can attest to their
usefulness. One example of their power is as an orthogonal means to return
multiple results from a function. Rather than a bizarre mixture of return
values and output parameters, a set of named values can be easily returned.
In D4 you can do stuff like this:
var name := GetName();
...name.First...
...name.Last...
there are also join, rename, and extend/project operators included with the
language:
GetName() { First FirstName /* rename */, First.ToUpper() UpperFirst /*
extend */ } join GetAddress()
/* result is a tuple (called a row in D4) with FirstName, UpperFirst, and
address attributes) */
...highly recommended!
Regards,
--
Nathan Allan
More information about the Digitalmars-d
mailing list