1/* 2 * Copyright (c) 2021-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 16function foo (parameter: number) { 17 let local: string = "function local"; 18 interface LocalInterface { // Local interface in a top-level function 19 method (): void; // It has a method 20 field: string; // and a property 21 } 22 class LocalClass implements LocalInterface { // Class implements interface 23 // Local class in a top-level function 24 override method () { 25 console.log ("Instance field = " + this.field + " par = " + parameter + " loc = " + local ) 26 assert(this.field == "`instance field value`") 27 assert(parameter == 42) 28 assert(local == "function local") 29 } 30 field: string = "`instance field value`" 31 static s_method () { 32 console.log ("Static field = " + LocalClass.s_field) 33 assert(LocalClass.s_field == "`class/static field value`") 34 35 } 36 static s_field: string = "`class/static field value`" 37 } 38 39 let lc: LocalInterface = new LocalClass(); 40 // Both local types can be freely used in the top-level function scope 41 lc.method() 42 LocalClass.s_method() 43} 44 45function main() : int 46{ 47 foo(42); 48 return 0; 49} 50