ETSI's Bug Tracker - TTCN-3 Change Requests | |||||
View Issue Details | |||||
ID | Project | Category | View Status | Date Submitted | Last Update |
0008113 | TTCN-3 Change Requests | New Feature | public | 17-08-2022 07:14 | 26-01-2024 16:27 |
Reporter | Matthias Simon | ||||
Assigned To | Jens Grabowski | ||||
Priority | normal | Severity | minor | Reproducibility | have not tried |
Status | assigned | Resolution | open | ||
Platform | OS | OS Version | |||
Clause Reference(s) | New Extension | ||||
For STF discussion | |||||
Source (company - Author) | Nokia -- Matthias Simon | ||||
Summary | 0008113: Type traits and user defined methods | ||||
Description | Type traits allow to compose behavior in a lightweight, but powerful way. ## Methods This extensions allows to specify methods for any user defined types. The receiver type is specified using the "for" keyword. Inside the behavior the receiver value is accessible via "this" symbol: module Example { type integer Timestamp function year() for Timestamp return string { return int2str(1970+this/SECONDS_PER_YEAR); } control { const Timestamp t := 1660681400; log(t.year()) // logs "2022" } } ## Traits A trait is a set of methods and can be defined using the "trait" keyword: trait Stringer { function String() charstring; } A variable of a trait type can hold any value that implements the trait: module Example { type record Point2D { integer x, integer y } function string() for Point3D return charstring { return sprintf("(%d|%d)", this.x, this.y) } type record Point3D { integer x, integer y, integer z } function string() for Point2D return charstring { return sprintf("(%d|%d|%d)", this.x, this.y, this.z) } trait Stringer { function string() charstring; } function logPoints(Stringer s) { log(s.string()) } control { var Point2D p1 := {1,2}; var Point3D p2 := {1,2,3} logPoints(p1); // okay because Point2D implements Stringer trait logPoints(p2); // okay because Point3D also implements Stringer trait } } ## Embedding When the field name is omitted, the field is called an embedded field: type integer Timestamp; external function year() for Timestamp return charstring; type record Date { Timestamp, // embedded field charstring zone // regular field } An embedded field is accessible by its type name: var Data d := { Timestamp := 1660681400, zone := "GMT+2" }; d.Timestamp := d.Timestamp + 3600; Embedded fields must be unique: type record Date { Timestamp, Timestamp // not allowed. } Methods of an embedded field are promoted and become methods of the embedding type: var Data d := { Timestamp := 1660681400, zone := "GMT+2" }; log(d.year()); // year is a promoted method implemented by the Timestamp type Conflicting promoted methods have to be resolved explicitly: type integer Duration; external function year() for Duration return charstring; type record Event { Timestamp, Duration } // Timestamp and Duration both provide a "year"-method. Event type need // to resolve this conflict explicitly: function year() for Event return charstring { return sprintf("start=%s, duration=%s", this.Timestamp, this.Duration) } ## Notes and Open Questions * Should we call it "trait" or rather "interface" like in Java, C# and Go? * Should we support default implementations for traits (requires "implements" keyword)? | ||||
Steps To Reproduce | |||||
Additional Information | |||||
Tags | No tags attached. | ||||
Relationships | |||||
Attached Files | |||||
Issue History | |||||
Date Modified | Username | Field | Change | ||
17-08-2022 07:14 | Matthias Simon | New Issue | |||
17-08-2022 07:16 | Matthias Simon | Description Updated | bug_revision_view_page.php?rev_id=587#r587 | ||
17-08-2022 09:53 | Jens Grabowski | Note Added: 0016228 | |||
17-08-2022 09:53 | Jens Grabowski | Assigned To | => Matthias Simon | ||
17-08-2022 09:53 | Jens Grabowski | Status | new => assigned | ||
08-11-2022 08:15 | Matthias Simon | Note Added: 0016267 | |||
08-11-2022 08:22 | Matthias Simon | Note Edited: 0016267 | bug_revision_view_page.php?rev_id=595 | ||
08-11-2022 08:23 | Matthias Simon | Note Edited: 0016267 | bug_revision_view_page.php?rev_id=596 | ||
08-11-2022 08:25 | Matthias Simon | Note Edited: 0016267 | bug_revision_view_page.php?rev_id=597 | ||
10-11-2022 09:05 | Matthias Simon | Note Edited: 0016267 | bug_revision_view_page.php?rev_id=600 | ||
10-11-2022 09:24 | Matthias Simon | Note Deleted: 0016267 | |||
10-11-2022 09:52 | Matthias Simon | Note Added: 0016287 | |||
10-11-2022 10:06 | Matthias Simon | Note Edited: 0016287 | bug_revision_view_page.php?bugnote_id=16287#r602 | ||
10-11-2022 10:46 | Tomas Urban | Note Added: 0016290 | |||
10-11-2022 12:50 | Matthias Simon | Note Edited: 0016287 | bug_revision_view_page.php?bugnote_id=16287#r603 | ||
07-12-2022 08:26 | Jens Grabowski | Note Added: 0016363 | |||
12-12-2022 09:01 | Matthias Simon | Note Added: 0016382 | |||
12-12-2022 14:37 | Matthias Simon | Note Added: 0016388 | |||
20-12-2022 11:45 | Jens Grabowski | Note Added: 0016437 | |||
08-11-2023 13:01 | Jens Grabowski | Note Added: 0016552 | |||
08-11-2023 13:01 | Jens Grabowski | Assigned To | Matthias Simon => Jens Grabowski | ||
26-01-2024 16:27 | Olivier Genoud | Note Added: 0016607 |
Notes | |||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|
||||
|
|||||
|
|