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

ID | Project | Category | View Status | Date Submitted | Last Update
0008070 | Part 01: TTCN-3 Core Language | New Feature | public | 06-12-2021 16:01 | 03-01-2023 11:50

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.2 The If-else statement
Source (company - Author) | Matthias Simon - Nokia

Summary | 0008070: If-else statements with initializers

Description

SUMMARY
Allow initializers in conditional statements, similar to for loops: if (init-statement; condition). This statement simplifies common code patterns and helps developers keep scopes tight.
MOTIVATION
In many cases we check a return value and perform conditional operations on this value. TTCN-3 code looks like this usually:
type record of charstring Users
var Users ret := getUsers()
if (lengthof(ret) > 0) {
// do something with "users"
}
There are two issues, because the variable "ret" leaks into surrounding scope:
1) You must take extra care not to access variables outside intended scope:
type record of charstring Users
var Users ret := getUsers()
if (lengthof(ret) > 0) {
// do something with "users"
}
var Users users = ret;
log(users[0]) // ERROR: index out of bounds, or worse.
2) You cannot reuse variable "ret" with different types in the same scope anymore:
var Users ret := getUsers()
if (lengthof(ret) > 0) {
// ...
}
var float ret := rnd() // ERROR: "ret" already defined.
if (ret > 0.5) {
// ...
}
PROPOSAL
**Semantic Description**
if "(" [ VarInstance ";"] BooleanExpression ")" StatementBlock
The branching of the control flow is decided upon the value of the Boolean expressions - the condition. A statement
block - and only one - will be executed, if its condition evaluates to true.
An optional variable (or constant) can be declared and initialized before being used in the if statement. The scope of the variable is limited to the statement block, i.e. it is only visible inside the statement block.
**Examples**
// Example 1: improved getUsers
type record of charstring Users
if (var Users ret := getUsers(); lengthof(ret) > 0 ) {
// do something with "users"
}
// Example 2: else-if statements
if (var charstring ret := hostname(); ret == "localhost") {
// do something with ret (charstring)
} else if (var float ret := rnd(); rnd > 0.4) {
// do something with ret (float)
} else {
// "ret" variable is not accessible in this scope
}
OPEN QUESTIONS
Should we allow initializers in switch-statements as well?
switch (var Person p := createPerson(); p.Name)
{
case ("Alice") { /* ... */ }
case ("Bob") { /* ... */ }
case else { /* ... */ }
}
What are the scopes of variable "p".

Steps To Reproduce |
Additional Information |
Tags | No tags attached.
Relationships |
Attached Files | CR8070.docx (118,037) 11-12-2022 12:26 http://oldforge.etsi.org/mantis/file_download.php?file_id=4092&type=bug

Issue History
Date Modified | Username | Field | Change
06-12-2021 16:01 | Matthias Simon | New Issue |
09-12-2021 09:07 | Jacob Wieland - Spirent | Note Added: 0016146 |
16-08-2022 14:17 | Jens Grabowski | Project | TTCN-3 Change Requests => Part 01: TTCN-3 Core Language
17-08-2022 11:14 | Jens Grabowski | Assigned To | => Matthias Simon
17-08-2022 11:14 | Jens Grabowski | Status | new => assigned
17-08-2022 11:17 | Jens Grabowski | Note Added: 0016235 |
11-12-2022 12:26 | Matthias Simon | File Added: CR8070.docx |
11-12-2022 12:26 | Matthias Simon | Note Added: 0016376 |
11-12-2022 12:26 | Matthias Simon | Assigned To | Matthias Simon => Axel Rennoch
11-12-2022 12:26 | Matthias Simon | Status | assigned => confirmed
12-12-2022 12:31 | Axel Rennoch | Note Added: 0016383 |
12-12-2022 12:31 | Axel Rennoch | Status | confirmed => resolved
12-12-2022 12:31 | Axel Rennoch | Resolution | open => fixed
12-12-2022 12:31 | Axel Rennoch | Assigned To | Axel Rennoch => Jens Grabowski
03-01-2023 11:50 | Jens Grabowski | Note Added: 0016446 |
03-01-2023 11:50 | Jens Grabowski | Status | resolved => closed

Notes

(0016146)
Jacob Wieland - Spirent
09-12-2021 09:07

I support this proposal. It could be a number of declarations, too.

(0016235)
Jens Grabowski
17-08-2022 11:17

TTF dicussion: Scope of variables needs to be studied. If we go for this approach, variables may be added to all constructs that define their own scope unit.

(0016376)
Matthias Simon
11-12-2022 12:26

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

(0016383)
Axel Rennoch
12-12-2022 12:31

The solution files looks correct.

(0016446)
Jens Grabowski
03-01-2023 11:50

Implemented as proposed.