1# Usage Rules and Constraints for Sendable 2 3## Inheritance Rules 4 5### Sendable Classes Must Inherit Only from Other Sendable Classes 6 7The layout and prototype chain of Sendable objects are immutable, whereas the layout of non-Sendable objects can be modified through special means. Therefore, mutual inheritance is not allowed. Note that this rule applies to classes, not variables; a Sendable class cannot inherit from a variable. 8 9**Correct Example** 10 11```ts 12@Sendable 13class A { 14 constructor() { 15 } 16} 17 18@Sendable 19class B extends A { 20 constructor() { 21 super() 22 } 23} 24``` 25<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/inheritonly/src/main/ets/pages/Index.ets) --> 26 27**Incorrect Example** 28 29```ts 30class A { 31 constructor() { 32 } 33} 34 35@Sendable 36class B extends A { // A is not a Sendable class. B cannot inherit it. A compilation error is reported. 37 constructor() { 38 super() 39 } 40} 41``` 42 43 44### Non-Sendable Classes Must Inherit Only from Other Non-Sendable Classes 45 46The layout and prototype chain of Sendable objects are immutable, whereas the layout of non-Sendable objects can be modified through special means. Therefore, mutual inheritance is not allowed. 47 48**Correct Example** 49 50```ts 51class A { 52 constructor() { 53 } 54} 55 56class B extends A { 57 constructor() { 58 super() 59 } 60} 61``` 62<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/inheritedfromnon/src/main/ets/pages/Index.ets) --> 63 64**Incorrect Example** 65 66```ts 67@Sendable 68class A { 69 constructor() { 70 } 71} 72 73class B extends A { // A is a Sendable class. B cannot inherit it. A compilation error is reported. 74 constructor() { 75 super() 76 } 77} 78``` 79 80## Interface Implementation Rules 81 82### Non-Sendable Classes Cannot Implement Sendable Interfaces 83 84If a non-Sendable class implements a Sendable interface, it may be mistakenly considered as Sendable, leading to incorrect usage. 85 86**Correct Example** 87 88```ts 89interface I {}; 90 91class B implements I {}; 92``` 93<!-- @[counter_example_achieve_non](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/achievenon/src/main/ets/pages/Index.ets) --> 94 95**Incorrect Example** 96 97```ts 98import { lang } from '@kit.ArkTS'; 99 100type ISendable = lang.ISendable; 101 102interface I extends ISendable {}; 103 104class B implements I {}; // I is a Sendable interface. B cannot implement it. A compilation error is reported. 105``` 106 107## Rules for Member Variables of Sendable Classes/Interfaces 108 109### Member Variables Must Be Sendable Data Types 110 111Sendable objects cannot hold non-Sendable data. Therefore, member properties of Sendable classes or interfaces must be of Sendable data types. 112 113**Correct Example** 114 115```ts 116@Sendable 117class A { 118 constructor() { 119 } 120 a: number = 0; 121} 122``` 123<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/variablesupport/src/main/ets/pages/Index.ets) --> 124 125**Incorrect Example** 126 127```ts 128@Sendable 129class A { 130 constructor() { 131 } 132 b: Array<number> = [1, 2, 3] // A compilation error is reported. Use collections.Array instead. 133} 134``` 135 136 137### Member Variables Cannot Use the Exclamation Mark (!) for Assertion 138 139Member properties of Sendable objects must be initialized. The assertion using the exclamation mark (!) allows variables to remain uninitialized. Therefore, using the exclamation mark (!) for assertion is not supported. 140 141**Correct Example** 142 143```ts 144@Sendable 145class A { 146 constructor() { 147 } 148 a: number = 0; 149} 150``` 151<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/variablenotsupported/src/main/ets/pages/Index.ets) --> 152 153**Incorrect Example** 154 155```ts 156@Sendable 157class A { 158 constructor() { 159 } 160 a!: number; // A compilation error is reported. The exclamation mark (!) is not supported. 161} 162``` 163 164 165### Member Variables Cannot Use Computed Property Names 166 167The layout of Sendable objects is immutable. Computed properties cannot statically determine the object layout, and therefore they cannot be used for Sendable objects. 168 169**Correct Example** 170 171```ts 172@Sendable 173class A { 174 num1: number = 1; 175 num2: number = 2; 176 add(): number { 177 return this.num1 + this.num2; 178 } 179} 180``` 181<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/nocalculationsupport/src/main/ets/pages/Index.ets) --> 182 183**Incorrect Example** 184 185```ts 186enum B { 187 b1 = "bbb" 188} 189@Sendable 190class A { 191 ["aaa"]: number = 1; // A compilation error is reported. ["aaa"] is not supported. 192 [B.b1]: number = 2; // A compilation error is reported. [B.b1] is not supported. 193} 194``` 195 196## Generic Rules 197 198### Template Types for Sendable Classes, SendableLruCache, collections.Array, collections.Map, and collections.Set Must Be Sendable 199 200Sendable objects cannot hold non-Sendable data. Therefore, template types for Sendable data in generic classes must be Sendable. 201 202**Correct Example** 203 204```ts 205import { collections } from '@kit.ArkTS'; 206 207try { 208 let arr1: collections.Array<number> = new collections.Array<number>(); 209 let num: number = 1; 210 arr1.push(num); 211} catch (e) { 212 console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`); 213} 214``` 215<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/templatetype/src/main/ets/pages/Index.ets) --> 216 217**Incorrect Example** 218 219```ts 220import { collections } from '@kit.ArkTS'; 221 222try { 223 let arr1: collections.Array<Array<number>> = new collections.Array<Array<number>>(); // A compilation error is reported. The template type must be Sendable. 224 let arr2: Array<number> = new Array<number>(); 225 arr2.push(1); 226 arr1.push(arr2); 227} catch (e) { 228 console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`); 229} 230``` 231 232 233## Context Access Rules 234 235### Sendable Classes Cannot Use Variables Defined in the Context of the Current Module 236 237Sendable objects operate in different concurrent instances with distinct context environments within a single virtual machine instance. Direct access to variables defined in the context of the current module can lead to unexpected behavior. Therefore, Sendable objects cannot use variables defined in the context of the current module. Violations will result in compile-time errors. 238 239> **NOTE** 240> 241> Since API version 12, Sendable classes can use top-level Sendable class objects. 242 243**Correct Example** 244 245```ts 246import { lang } from '@kit.ArkTS'; 247 248type ISendable = lang.ISendable; 249 250interface I extends ISendable {} 251 252@Sendable 253class B implements I { 254 static o: number = 1; 255 static bar(): B { 256 return new B(); 257 } 258} 259 260@Sendable 261class C { 262 v: I = new B(); 263 u: number = B.o; 264 265 foo() { 266 return B.bar(); 267 } 268} 269``` 270<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/notallowedInside/src/main/ets/pages/Index.ets) --> 271 272**Incorrect Example** 273 274```ts 275import { lang } from '@kit.ArkTS'; 276 277type ISendable = lang.ISendable; 278 279interface I extends ISendable {} 280 281@Sendable 282class B implements I {} 283 284function bar(): B { 285 return new B(); 286} 287 288let b = new B(); 289 290{ 291 @Sendable 292 class A implements I {} 293 294 @Sendable 295 class C { 296 u: I = bar(); // bar is not a Sendable class object. A compile-time error is reported. 297 v: I = new A(); // A is not defined at the top level. A compile-time error is reported. 298 299 foo() { 300 return b; // b is not a Sendable class object but an instance of the Sendable class. A compile-time error is reported. 301 } 302 } 303} 304``` 305 306## Rules for Using the \@Sendable Decorator 307 308### The @Sendable Decorator Can Only Be Used to Decorate Classes and Functions 309 310Currently, only classes and functions can be decorated. 311 312**Correct Example** 313 314```ts 315@Sendable 316type SendableFuncType = () => void; 317 318@Sendable 319class C {} 320 321@Sendable 322function SendableFunc() { 323 console.info("Sendable func"); 324} 325``` 326<!-- @[counter_example_only_support](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/achievenon/src/main/ets/pages/Index.ets) --> 327 328**Incorrect Example** 329 330```ts 331@Sendable 332type A = number; // A compile-time error is reported. 333 334@Sendable 335type D = C; // A compile-time error is reported. 336``` 337 338### Sendable Classes and Functions Cannot Use Decorators Other Than @Sendable 339 340If a class decorator is defined in a TS file, the class layout may be modified, causing runtime errors. 341 342**Correct Example** 343 344```ts 345@Sendable 346class A { 347 num: number = 1; 348} 349``` 350<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/cannotbeused/src/main/ets/pages/Index.ets) --> 351 352**Incorrect Example** 353 354```ts 355@Sendable 356@Observed // A compilation error is reported. 357class C { 358 num: number = 1; 359} 360``` 361 362## Initialization Rules 363 364### Object Literals/Array Literals Cannot Be Used to Initialize Sendable Objects 365 366Object literals and array literals are not Sendable types. Sendable data types must be created using **new** expressions of Sendable types. 367 368**Correct Example** 369 370```ts 371import { collections } from '@kit.ArkTS'; 372 373let arr1: collections.Array<number> = new collections.Array<number>(1, 2, 3); // The type is Sendable. 374``` 375<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/objectliterals/src/main/ets/pages/Index.ets) --> 376 377**Incorrect Example** 378 379```ts 380import { collections } from '@kit.ArkTS'; 381 382let arr2: collections.Array<number> = [1, 2, 3]; // The type is not Sendable. A compile-time error is reported. 383let arr3: number[] = [1, 2, 3]; // The type is not Sendable. No error is reported. 384let arr4: number[] = new collections.Array<number>(1, 2, 3); // A compile-time error is reported. 385``` 386 387## Type Conversion Rules 388 389### Non-Sendable Types Cannot Be Cast to Sendable Types 390 391Except for the Object type, non-Sendable types cannot be forcibly converted to Sendable types. Using **as** to cast a non-Sendable type to a Sendable type results in an object that is still non-Sendable, leading to incorrect usage. Sendable types, however, can be cast to non-Sendable types using **as** to maintain compatibility, provided they do not violate Sendable rules. 392 393**Correct Example** 394 395```ts 396class A { 397 state: number = 0; 398} 399 400@Sendable 401class SendableA { 402 state: number = 0; 403} 404 405let a1: A = new SendableA() as A; 406``` 407<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/typecannot/src/main/ets/pages/Index.ets) --> 408 409**Incorrect Example** 410 411```ts 412class A { 413 state: number = 0; 414} 415 416@Sendable 417class SendableA { 418 state: number = 0; 419} 420 421let a2: SendableA = new A() as SendableA; // A compilation error is reported. 422``` 423 424## Function Rules 425 426### Arrow Functions Cannot Be Marked as Sendable 427 428Arrow functions do not support the @Sendable decorator and are non-Sendable. Therefore, they cannot be shared. 429 430**Correct Example** 431 432```ts 433@Sendable 434type SendableFuncType = () => void; 435 436@Sendable 437function SendableFunc() { 438 console.info("Sendable func"); 439} 440 441@Sendable 442class SendableClass { 443 constructor(f: SendableFuncType) { 444 this.func = f; 445 } 446 func: SendableFuncType; 447} 448 449let sendableClass = new SendableClass(SendableFunc); 450``` 451<!-- @[counter_example](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/RulesAndRestrictions/arrowfunctions/src/main/ets/pages/Index.ets) --> 452 453**Incorrect Example** 454 455```ts 456@Sendable 457type SendableFuncType = () => void; 458let func: SendableFuncType = () => {}; // A compile-time error is reported. 459 460@Sendable 461class SendableClass { 462 func: SendableFuncType = () => {}; // A compile-time error is reported. 463} 464``` 465 466## Rules for Interaction with TS/JS 467 468### ArkTS General Rules (Only for Sendable Objects Currently) 469 470| Rule| 471| -------- | 472| Do not modify the object layout (add or delete properties, or change property types) of Sendable objects when passing them to TS/JS interfaces.| 473| Do not modify the object layout (add or delete properties, or change property types) of Sendable objects when setting them to TS/JS objects.| 474| Do not modify the object layout (add or delete properties, or change property types) of Sendable objects when placing them in TS/JS containers.| 475 476> **NOTE** 477> 478> Changing the property type does not include changing the type of a Sendable object, such as from Sendable class A to Sendable class B. 479 480 481### NAPI Rules (Only for Sendable Objects Currently) 482 483For details about Node-APIs, see [Sendable-related Operations](../napi/use-napi-about-extension.md#sendable-related-operations). For details about how to use the Node-APIs, see [Wrapping a Native Object in a Sendable ArkTS Object](../napi/use-sendable-napi.md). 484 485| Rule| 486| -------- | 487| Do not delete properties. Prohibited interfaces: **napi_delete_property**.| 488| Do not add properties. Prohibited interfaces: **napi_set_property**, **napi_set_named_property**, and **napi_define_properties**.| 489| Do not change property types. Prohibited interfaces: **napi_set_property**, **napi_set_named_property**, and **napi_define_properties**.| 490| Symbol-related interfaces and types are not supported. Prohibited interfaces: **napi_create_symbol**, **napi_is_symbol_object**, and **napi_symbol**.| 491 492 493## Rules for Interaction with the UI 494 495To observe data changes in Sendable objects when interacting with UI, Sendable data must be used in conjunction with [makeObserved](../ui/state-management/arkts-new-makeObserved.md). For more information, see [Using makeObserved and @Sendable Decorated Class Together](../ui/state-management/arkts-new-makeObserved.md#using-makeobserved-and-sendable-decorated-class-together). 496 497 498## Rules for Using Sendable in HARs 499 500When using Sendable in HAR, you must enable the configuration for compiling and generating TS files. For details, see [Building TS Files](../quick-start/har-package.md#building-ts-files). 501 502<!--no_check-->