Logo etsi

ETSI's Bug Tracker

Notice: information submitted on the ETSI issue Tracker may be incorporated in ETSI publication(s) and therefore subject to the ETSI IPR policy.

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0008188Ext Pack: Behaviour Types (ES 202 785)New Featurepublic11-01-2023 12:5726-01-2024 16:28
ReporterMatthias Simon 
Assigned ToGusztáv Adamis 
PrioritynormalSeverityminorReproducibilityhave not tried
StatusassignedResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0008188: Support for function literals
DescriptionA function literal, also known as lambda function or anonymous function, is a function definition without name. Example:

    var fn := function (integer x) return boolean { return x mod 2 == 0 }
    apply(fn(23));
TagsNo tags attached.
Clause Reference(s)n/a
Source (company - Author)Nokia - Matthias Simon
Attached Files

- Relationships

-  Notes
(0016522)
Jens Grabowski (manager)
05-09-2023 11:06

TTF discussion: More elaborated example needed.
(0016549)
Matthias Simon (developer)
08-11-2023 12:35

Function literals are evaluated lazily and capture their context. This makes implementation of deferred functions feasible. For example:
 

    type function CancelFunc();

    testcase Example() runs on MTC {
        // start fixtures
        var cancel := SetupDatabase();

        // do some testing

        // handle graceful shutdown
        cancel();
    }

    function SetupDatabase() return CancelFunc
    {
        var db := DatabaseComponent.create;
        db.start(listen());
        return function() {
            db.stop; // Captures db and allows to call methods, locally
        }
    }


Another use-case is parametrizing callbacks:

    module Service
    {
        type component Component { /* ... */ }

        function Start(in ExitFunc ef := withSuccess) return Component
        {
            var c := Component.create;
            c.start(mainLoop(ef));
            return c;
        }

        private function mainLoop(ExitFunc ef) runs on Component {
            p.receive(ProcessState:{started := ?});
            log("Service is up and running");

            var ExitCode ec;
            p.receive(ProcessState:{exited := ?}) -> value ec;
            f(ec);
        }

        type function ExitFunc(ExitCode ec) runs on Component;

        type enumerated ExitCode {
            SUCCESS(0),
            ERROR(1..127),
            SIGTERM(-15),
            SIGKILL(-9),
        }

        group helpers
        {
            function withSuccess(in ExitCode ec) runs on Component {
                if (ec != SUCCESS) {
                    setverdict(fail, "want=0, got=" & ec);
                    stop;
                }
            }

            function withError(in ExitCode ec) runs on Component {
                if (ec != ERROR) {
                    setverdict(fail, "want!=0, got=" & ec);
                    stop;
                }
            }

            function withSignal(in ExitCode sig) return ExitFunc
            {
                return function(in ExitCode ec) runs on Component {
                    if (ec != sig) {
                        setverdict(fail, "want=" & sig, got=" & ec);
                        stop;
                    }
                }
            }

            function withTimeout(in float duration) return ExitFunc
            {
                return function(in ExitCode ec) runs on Component {
                    // ...
                }
        }
    }

    module Example
    {
        testcase SunnyDay()
        {
            var service1 := Service.Start();
            var service2 := Service.Start(Service.withError);

            // do some API testing...

            all components.done;
        }


        testcase ResilianceTest_ServiceCrashes()
        {
            var service1 := Service.Start(Service.withSignal(Service.SIGTERM));
            var service2 := Service.Start(Service.withTimeout(60.0));

            // do some API testing...

            all components.done;
        }

    }
(0016569)
Gusztáv Adamis (developer)
09-11-2023 20:52

The problems in examples can be solved with the existing TTCN-3 instructions.
(0016610)
Olivier Genoud (administrator)
26-01-2024 16:28

We assume/expect that this CR will not become part of the core language but will become part of an extension or be added to an extension (e.g. OO extension).

- Issue History
Date Modified Username Field Change
11-01-2023 12:57 Matthias Simon New Issue
05-09-2023 11:06 Jens Grabowski Note Added: 0016522
05-09-2023 11:07 Jens Grabowski Assigned To => Matthias Simon
05-09-2023 11:07 Jens Grabowski Status new => assigned
08-11-2023 12:35 Matthias Simon Note Added: 0016549
08-11-2023 13:54 Jens Grabowski Assigned To Matthias Simon => Jens Grabowski
08-11-2023 13:54 Jens Grabowski Assigned To Jens Grabowski => Matthias Simon
08-11-2023 13:54 Jens Grabowski Assigned To Matthias Simon => Gusztáv Adamis
09-11-2023 20:52 Gusztáv Adamis Note Added: 0016569
26-01-2024 16:28 Olivier Genoud Note Added: 0016610


MantisBT 1.2.14 [^]
Copyright © 2000 - 2024 MantisBT Team
Powered by Mantis Bugtracker