• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /*---
17desc: A rest parameter allows to make a function or method that take an unbounded
18  number of arguments. A rest parameter is marked with ... symbol before parameter
19  name.
20name: spec/rest-params/RestParamsTest_PassLambdas_1.ets
21
22---*/
23
24class Base {
25
26}
27
28type StringToInt = (val: String) => int;
29
30class Testee extends Base {
31
32    public callLambda(prefix: String, ...lValues: StringToInt[]): int {
33        let sum: int = 0;
34        for(let call of lValues) {
35            if(call != null) sum += call(prefix);
36        }
37        return sum;
38    }
39
40    public callLambda(...lValues: StringToInt[]): int {
41        let sum: int = 0;
42        for(let call of lValues) {
43            if(call != null) sum += call("Hello");
44        }
45        return sum;
46    }
47
48
49}
50
51function main(): int {
52
53    let test = new Testee();
54    let result: int = 0;
55
56    let l1: StringToInt = (str: String) => { return str.length as int }
57    result = test.callLambda("Hello", l1, l1, (str: String) => { return str.length as int }, null as StringToInt);
58    if(result != 15) return 1;
59
60    return 0;
61
62}
63