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/RRestParamsTest_PassLambdas_0.ets 21 22---*/ 23 24class Base { 25} 26 27type StringToInt = (val: String) => int; 28 29class Testee extends Base { 30 31 public callLambda(...lValues: StringToInt[]): int { 32 let sum: int = 0; 33 for(let call of lValues) { 34 if(call != null) sum += call("Hello"); 35 } 36 return sum; 37 } 38 39 public callLambda(prefix: String, ...lValues: StringToInt[]): int { 40 let sum: int = 0; 41 for(let call of lValues) { 42 if(call != null) sum += call(prefix); 43 } 44 return sum; 45 } 46} 47 48function main(): int { 49 50 let test = new Testee(); 51 let result: int = 0; 52 53 let l1: StringToInt = (str: String) => { return str.length as int } 54 result = test.callLambda(l1, l1, (str: String) => { return str.length as int }, null as StringToInt); 55 if(result != 15) return 1; 56 57 return 0; 58 59} 60