1/* 2 * Copyright (c) 2024 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 InnerValue { 17 private _v: int = 0 18 get v(): int { return this._v; } 19 set v(v: int){ this._v = v; } 20} 21 22class C { 23 private _x: int = 0 24 get x(): int { return this._x; } 25 set x(x: int){ this._x = x; } 26 27 private _iv: InnerValue = {v: 0} 28 get iv(): InnerValue { return this._iv; } 29 set iv(iv:InnerValue){ this._iv = iv; } 30} 31 32function returnC(): C { 33 return {x: 99, iv: {v: 77}} // return statement 34} 35 36function test(c: C = {}, x: int = 0, ivv: int = 0) { 37 assert c.x == x : "c.x != x" 38 assert c.iv.v == ivv : "c.iv.v != ivv" 39} 40 41function main(): int { 42 let c: C = {"x": 7, iv: {v: 8}}; // variable definition 43 test(c, 7, 8) 44 45 test() // optional parameter 46 47 let c2 = { // as construction 48 x: 4, 49 iv: {v: 5} 50 } as C; 51 test(c2, 4, 5) 52 53 c = {x: 5, iv: {v: 6}} // assignment 54 test(c, 5, 6) 55 56 test({ // function argument 57 x: 3, 58 }, 3, 0) 59 60 test(returnC(), 99, 77) 61 62 let ca: C[] = [{x: 42, iv: {v: 1}}, {x: 128, iv: {v: 2}}] // array elements 63 test(ca[1], 128, 2) 64 return 0; 65} 66