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 16class MyType { 17 x: int = 10; 18 constructor() { } 19 constructor(a: int) { 20 this.x = a; 21 } 22} 23 24function main(): void { 25 assertEQ(foo1(), 8) 26 27 assertEQ(foo2(), 30) 28 assertEQ(foo2(new MyType(5)), 25) 29 30 assertEQ(foo3(new MyType(5)), 55) 31 assertEQ(foo3(new MyType(5), new MyType(10) ), 45) 32 33 assertEQ(foo4(), 0) 34 35 assertEQ(foo5(), 0) 36 assertEQ(foo5(new MyType(5)), -1) 37 38 assertEQ(foo6(new MyType(5)), 0) 39 assertEQ(foo6(new MyType(5), new MyType(10)), -1) 40 41 assertEQ(foo7(), 0) 42 assertEQ(foo8(), 0) 43 44 assertEQ(foo9(new MyType(10)), 15) 45 assertEQ(foo10(new MyType(10)), 25) 46 47 assertEQ(foo11(new MyType(10), new MyType(5)), 20) 48 assertEQ(foo12(new MyType(10), new MyType(5)), 30) 49} 50 51function foo1(a: MyType|null = new MyType(8)): int { 52 if (a == null) { 53 return -1; 54 } 55 return a.x; 56} 57 58function foo2(a: MyType = new MyType(10), b: MyType = new MyType(20)): int { 59 return a.x + b.x; 60} 61 62function foo3(a: MyType = new MyType(10), b: MyType = new MyType(20), c: MyType = new MyType(30)): int { 63 assertEQ(a.x, 5) 64 65 return a.x + b.x + c.x; 66} 67 68function foo4(a?: MyType): int { 69 if (a == null) { 70 return 0; 71 } 72 return a.x; 73} 74 75function foo5(a?: MyType, b?: MyType): int { 76 if (a == null && b == null) { 77 return 0; 78 } 79 if (b == null) { 80 return -1; 81 } 82 return a!.x + b.x; 83} 84 85function foo6(a?: MyType, b?: MyType, c?: MyType): int { 86 assertEQ(a!.x, 5) 87 if (b == null && c == null) { 88 return 0; 89 } 90 if (c == null) { 91 return -1; 92 } 93 94 return a!.x + b!.x + c.x; 95} 96 97function foo7(a: MyType = new MyType(5), b?: MyType): int { 98 if (b == null) { 99 return 0; 100 } 101 return a.x + b.x; 102} 103 104function foo8(a?: MyType, b: MyType = new MyType(5), c?: MyType): int { 105 assertEQ(b.x, 5) 106 107 if (a == null && c == null) { 108 return 0; 109 } 110 111 return a!.x + b.x + c!.x; 112} 113 114function foo9(a: MyType, b: MyType = new MyType(5)): int { 115 return a.x + b.x; 116} 117 118function foo10(a: MyType, b: MyType = new MyType(5), c: MyType = new MyType(10)): int { 119 return a.x + b.x + c.x; 120} 121 122function foo11(a: MyType, b: MyType, c: MyType = new MyType(5)): int { 123 return a.x + b.x + c.x; 124} 125 126function foo12(a: MyType, b: MyType, c: MyType = new MyType(10), d: MyType = new MyType(5)): int { 127 return a.x + b.x + c.x + d.x; 128} 129