[fpc-devel] for-in-index loop
Alexander Klenin
klenin at gmail.com
Fri Jan 25 22:44:38 CET 2013
On Sat, Jan 26, 2013 at 5:30 AM, Florian Klämpfl <florian at freepascal.org> wrote:
> Where? Concrete code of a serious language! Not some "oh, yes, this
> language has it and that as well"
On Sat, Jan 26, 2013 at 7:34 AM, Florian Klämpfl <florian at freepascal.org> wrote:
> No. I want to see a language which provides something like the explicit
> index keyword and not some tupel returning enumerator like python which
> can be done already.
Oh, I see. Please, state your questions more clearly -- there is no
way I could infer
the second question from the first one!
There are two answers here:
1) If you would look at the examples I provided, you would notice that
while Python
(and C++ which I did not list) do use the concept of tuple, other
listed languages do not.
2) Indeed, introducing tuples to Pascal might be an alternative
solution. Below is a proposal:
2.1) Tuple definition. Tuple is an anonymous list of values, possibly
of different types. It is impossible to explicitly declare tuple type,
or store a tuple in a variable.
2.2) Tuple construction: Tuple may be constructed by listing its
elements in as a semicolon-separated list in parenthesis:
(a; b; c). Also, any value of record or array type can be converted
to a tuple by using built-in pseudo-function Tuple
(or other some name, perhaps even a special character like ~ or @@).
Semicolon is chosen to underline similarity with the record
constants -- but perhaps comma is better.
Alternatively, a square brackets may be used since they already
represent similar semantics for "array of const" parameters.
2.3) Tuple assignment. Tuple may be assigned to a record, provided
field types match. Most important, tuples may be used in
deconstructing assignment by listing variables in the same syntax on
the left side of assignment:
(a; b) := (x; y);
If the left side contains less variables then the tuple has values,
extra values are ignored.
If the right side contains more variables, it is an error.
2.4) Tuples as procedure arguments. Tuple may be passed to a procedure
or function.
In this case, deconstructing assignment is performed from the tuple to
the actual arguments,
as if they were listed on the left side of assignment in the previous point.
Also, a tuple may be present inside square brackets representing
"array of const" argument.
In this case, it works as if tuple elements were listed instead.
2.5) for-in loop accepts a list of variables after "for" keyword. In
this case, it tries to convert the Current value to a tuple and
perform tuple assignment.
With this proposal, following benefits are gained:
1) Group assignment:
(x; y; z) := (0; 0; 0);
(a; b) := (b; a);
(first; second) := Tuple(SomeFunctionReturningArray());
2) Record literals without constructor functions:
ARectangle.TopLeft := (100; 200);
3) Formatting complex values:
s := Format('%d-%d', [Tuple(CenterPoint(ARectangle))]);
4) Collecting default values for procedures:
In some library:
procedure WithManyParams(A, B, C, D, E, F: Integer);
In user code:
var
p: record A, B, C, D, E, F: Integer; end;
p := (default; values; for; params);
...
p.E := 5;
WithManyParams(Tuple(p));
5) Finally, for-in:
for (v; i) in a do ...
Compiler code for built-in types mostly the same as for-in-index --
syntax difference only.
User-defined iterators may return records with fields (value, key).
In the case of
for v in a
v may now be either a record or just a value, depending on the type.
--
Alexander S. Klenin
More information about the fpc-devel
mailing list