A Discussion of Tuple Syntax
Kenji Hara
k.hara.pg at gmail.com
Mon Aug 19 20:33:38 PDT 2013
Sorry I cannot reply to each thread comments quickly, so I discharge my
opinions at once.
----
D's built-in tuple can contain following entities:
- Type
int, long, array types, AA types, user-defined types, etc
- Expressions
interger, string literal, etc
- Symbol
user-defined types, templates, template instances, etc
Note that: an user-defined type would be treated as both Type and Symbol.
template Test() {}
alias X = std.typetuple.TypeTuple!(
int, long, char[], int[int], const Object, // Types
1, "str", [1,2,3], // Expressions (literal values)
object.Object, Test, Test!(), // Symbols
);
If all of the elements in a built-in tuple are Type, today it is normally
called "Type Tuple".
If all of the elements in a built-in tuple are Expressions, today it is
normally called "Expression Tuple".
Note that: today we cannot create a built-in tuple without using
TemplateTupleParameter.
TemplateTupleParameter cannot take expressions non-literal expressions,
therefore
most of current built-in tuples would contains only literal values as the
Expressions.
----
std.typecons.Tuple is an artifact of built-in tuple + alias this.
struct Tuple(T...) {
T expand; // 'expand' is a built-in tuple of
// the implicitly defined fields that
// typed T[0], T[1], ... T[$-1].
// In spec, this is called "TupleDeclaration".
alias expand this; // forward indexing/slicing operators to
// the TupleDeclaration 'expand'
}
Tuple!(int, string) t;
t[]; // t.expand[]
t[0..$]; // t.expand[0..$]
t[1]; // t.expand[1]
So, std.typecons.Tuple _is not special_. You can define another Tuple
struct in the same way.
We should not define new syntax for the library utility std.typecons.Tuple.
If you want to return multiple values from a function, you must always wrap
them by std.typecons.Tuple, or other used-defined types. You cannot
directly return built-in tuple from a function.
(Built-in tuple return is mostly equivalent with multiple-value-return
issue. However it would be mostly impossible that defining calling
conversion scheme for that in portable)
----
The combination of built-in tuple and alias this would be one of the case
of built-in tuple of non-literal Expressions.
For example, `t.expand[1]` is equivalent with the dot expression
`t.__field_1` which cannot be taken by TemplateTupleParameter.
Therefore, `t.expand` would be the built-in tuple of the two dot
expressions `t.__field_0` and `t.__field_1`.
Therefore, calling built-in tuple "Alias Tuple" would not be correct so
built-in tuple can contain expressions that cannot be aliased.
----
My opinions agains various syntax proposals:
#(1, "str")
--> The character '#' is already used for the start of "Special Token
Sequences"
http://dlang.org/lex.html#Special Token Sequence
It is recognized in lexing phase, so adding semantic meaning to the '#'
character would be a contradict of D's principle.
Quote from http://dlang.org/lex.html
"The lexical analysis is independent of the syntax parsing and the semantic
analysis."
Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130820/e594044b/attachment-0001.html>
More information about the Digitalmars-d
mailing list