ETSI's Bug Tracker - Part 01: TTCN-3 Core Language
View Issue Details

ID | Project | Category | View Status | Date Submitted | Last Update
0008069 | Part 01: TTCN-3 Core Language | New Feature | public | 06-12-2021 15:04 | 03-01-2023 12:44

Reporter | Matthias Simon |
Assigned To | Jens Grabowski |
Priority | normal | Severity | minor | Reproducibility | have not tried
Status | closed | Resolution | fixed |
Platform |  | OS |  | OS Version |
Product Version |  |
Target Version |  | Fixed in Version |  |
Clause Reference(s) | 19.4 The For statement
Source (company - Author) | Matthias Simon - Nokia

Summary | 0008069: Range-based for loop

Description

SUMMARY
Allow range clause as a second form of for-loops, to improve developer convenience and readability of TTCN-3 source code.
MOTIVATION
Many programming languages (C++, Python, Go, Perl, Rust, ...) provide ranged loops as a concise and convenient alternative to the classic C-style loop.
Most developers would probably benefit from having this kind of loop in TTCN-3, as well:
The Nokia TTCN-3 code base for 4G and 5G C-Plane contains over 14000 for-loops; 99 percent could be simplified into ranged loops. Other TTCN-3 code bases show a similar picture [1].
A key idea of TTCN-3 is having the look and feel of a regular programming language, without distracting complexity [2]. A ranged for-loop reduces complexity.
PROPOSAL
**Syntactical Structure**
for "(" (VarInstance|ValueRef) ":=" "range" Expression ")" StatementBlock
**Semantic Description**
A _for_ statement with a range clause iterates through all entries of a _record of_, _set of_, _array_, _map_ type or string type (bitstring, hextstring, octettstring, charstring, universal charstring). For each entry it assigns an iteration value to corresponding iteration variable and then executed the statement block.
Iteration values must be compatible with the iteration variable. Rules for accessing single elements apply (15.6, ...).
The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next.
The expression on the right in the "range" clause is evaluated once before beginning the loop.
**Examples**
// Iterating over an array variable
var charstring myArray[3] := { "foo", "bar", "baz" }
for (var charstring s := range myArray) {
// ...
}
// Iterating over maps
var map from charstring to boolean visited := {
["A"] := true,
["B"] := true,
["C"] := true,
}
// Use "from" to iterate over map keys
for (var charstring k := range visited.from) {
// ...
}
// Use "to" to iterate over map values
for (var charstring v := range visited.to) {
// ...
}
// Iterating over expressions and literal values
for (var integer x := range {1, 1, 2} & {3, 5, 8}) {
// ...
}
// Modifying length has no impact on iteration,
// because range expression is evaluated only once.
type record of integer RoI;
var RoI ints := {2, 4}
for (var integer i := range ints) {
ints[lengthof(ints)] = i*3 // append
}
log(int) // {2, 4, 6, 12}
// Iterating over templates could use some examples
OPEN QUESTIONS
**Keywords**
It might be a good idea to reuse existing keywords, instead of adding a new one. Some suggestions are:
* "all from": for (var integer i := all from myArray)
* "repeat": for (var integer i := repeat myArray)
* "select": for (var integer i := select myArray)
* no keyword: for (var integer i := myArray)
**Expanding Values**
Expanding value lists in range loops could be an interesting feature worth exploring:
// Example 1:
for (var integer i := range (0..2, 4, 6) {
// this loop will repeat five times: i:= 0, 1, 2, 4, 6
}
// Example 2: generating permutations
for (var RoI r := range permutation({1,2,3}) {
// this loop will repeat six time: r := {1, 2, 3}, {2, 1, 3}, {2, 3, 1}, ...
}
REFERENCES
[1](https://github.com/osmocom/osmo-ttcn3-hacks [^])
[2](http://www.ttcn-3.org/index.php/about/why-use-ttcn3 [^])

Steps To Reproduce |
Additional Information |
Tags | No tags attached.
Relationships |
Attached Files | CR8069.docx (117,554) 11-12-2022 09:06 http://oldforge.etsi.org/mantis/file_download.php?file_id=4091&type=bug CR8069(1).docx (117,436) 12-12-2022 14:23 http://oldforge.etsi.org/mantis/file_download.php?file_id=4097&type=bug CR8069(2).docx (136,085) 13-12-2022 15:11 http://oldforge.etsi.org/mantis/file_download.php?file_id=4098&type=bug

Issue History
Date Modified | Username | Field | Change
06-12-2021 15:04 | Matthias Simon | New Issue |
09-12-2021 09:02 | Jacob Wieland - Spirent | Note Added: 0016145 |
09-12-2021 12:51 | Matthias Simon | Note Added: 0016147 |
16-08-2022 14:17 | Jens Grabowski | Project | TTCN-3 Change Requests => Part 01: TTCN-3 Core Language
17-08-2022 11:31 | Jens Grabowski | Note Added: 0016236 |
17-08-2022 11:31 | Jens Grabowski | Assigned To | => Matthias Simon
17-08-2022 11:31 | Jens Grabowski | Status | new => assigned
11-12-2022 09:06 | Matthias Simon | File Added: CR8069.docx |
11-12-2022 09:08 | Matthias Simon | Note Added: 0016373 |
11-12-2022 09:08 | Matthias Simon | Note Added: 0016374 |
11-12-2022 09:08 | Matthias Simon | Assigned To | Matthias Simon => Axel Rennoch
11-12-2022 09:08 | Matthias Simon | Status | assigned => confirmed
11-12-2022 09:08 | Matthias Simon | Note Deleted: 0016373 |
11-12-2022 09:25 | Matthias Simon | Note Added: 0016375 |
12-12-2022 12:47 | Axel Rennoch | Note Added: 0016384 |
12-12-2022 12:47 | Axel Rennoch | Assigned To | Axel Rennoch => Matthias Simon
12-12-2022 12:47 | Axel Rennoch | Status | confirmed => assigned
12-12-2022 14:23 | Matthias Simon | Note Added: 0016386 |
12-12-2022 14:23 | Matthias Simon | File Added: CR8069(1).docx |
12-12-2022 14:24 | Matthias Simon | Note Added: 0016387 |
12-12-2022 14:24 | Matthias Simon | Assigned To | Matthias Simon => Axel Rennoch
12-12-2022 14:24 | Matthias Simon | Status | assigned => confirmed
12-12-2022 14:25 | Matthias Simon | Note Deleted: 0016386 |
13-12-2022 15:11 | Axel Rennoch | File Added: CR8069(2).docx |
13-12-2022 15:13 | Axel Rennoch | Note Added: 0016389 |
13-12-2022 15:13 | Axel Rennoch | Status | confirmed => resolved
13-12-2022 15:13 | Axel Rennoch | Resolution | open => fixed
13-12-2022 15:13 | Axel Rennoch | Assigned To | Axel Rennoch => Jens Grabowski
03-01-2023 12:44 | Jens Grabowski | Note Added: 0016448 |
03-01-2023 12:44 | Jens Grabowski | Status | resolved => closed

Notes

(0016145)
Jacob Wieland - Spirent
09-12-2021 09:02

how about for (var <type> <name> from <record of/set of/array>) { ... }

(0016147)
Matthias Simon
09-12-2021 12:51

What about: for (var <type> <name> in <record of/set of/array>) { ... } The "in" form looks a little more python-ish. I am missing ":=", though. Maybe it's just habit, but: for (var Rec r := all from myArray) { ... } looks more familiar than: for (var Rec r from myArray) { ...} One (minor) advantage of keeping the ":=" is less syntactic variation.

(0016236)
Jens Grabowski
17-08-2022 11:31

TTF discussion: Useful feature. Details to be studied.

(0016374)
Matthias Simon
11-12-2022 09:08

The first version of the resolution uploaded. Please check and let me know what should be changed or added.

(0016375)
Matthias Simon
11-12-2022 09:25

NOTE: There's an issue with iterating template strings. For example:
template hexstring t := '1*D'H;
for (var x in t) {
log(x);
}
Above code could be translated to:
_r := t;
_l := lengthof(_r) // <-- here's the problem
for (var integer _i := 0; _i<_l; i++) {
var x := _r[_i];
log(x);
}
Problem: We cannot retrieve the length of template strings for the iteration, because lengthof('1*D'H) causes an error.

(0016384)
Axel Rennoch
12-12-2022 12:47

It would be better to separate the different loop forms using different subsections 19.4.1 and 19.4.2. According to the reported note #16375 the solution contains an issue that needs to be solved in the solution first.

(0016387)
Matthias Simon
12-12-2022 14:24

Updated version using different subsections. The lengthof issue is independent of this CR and can be solved in an new CR independently: http://oldforge.etsi.org/mantis/view.php?id=8155 [^]

(0016389)
Axel Rennoch
13-12-2022 15:13

The proposed solution file looks right and just has been updated a bit for editorial issues.

(0016448)
Jens Grabowski
03-01-2023 12:44

Implemented as proposed