1# Sendable Usage Rules and Constraints 2 3## A sendable class can inherit only from another sendable class. 4 5> **Note** 6> 7> The class here does not include variables. In other words, a sendable class cannot inherit from a variable. 8 9**Positive 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 26**Negative Example:** 27 28```ts 29class A { 30 constructor() { 31 } 32} 33 34@Sendable 35class B extends A { 36 constructor() { 37 super() 38 } 39} 40``` 41 42 43## A non-sendable class can inherit only from a non-sendable class. 44 45**Positive Example:** 46 47```ts 48class A { 49 constructor() { 50 } 51} 52 53class B extends A { 54 constructor() { 55 super() 56 } 57} 58``` 59 60**Negative Example:** 61 62```ts 63@Sendable 64class A { 65 constructor() { 66 } 67} 68 69class B extends A { 70 constructor() { 71 super() 72 } 73} 74``` 75 76 77## A non-sendable class can implement only a non-sendable interface. 78 79**Positive Example:** 80 81```ts 82interface I {}; 83 84class B implements I {}; 85``` 86 87**Negative Example:** 88 89```ts 90import { lang } from '@kit.ArkTS'; 91 92type ISendable = lang.ISendable; 93 94interface I extends ISendable {}; 95 96class B implements I {}; 97``` 98 99 100## The member variables of a sendable class or interface must be of a sendable data type. 101 102**Positive Example:** 103 104```ts 105@Sendable 106class A { 107 constructor() { 108 } 109 a: number = 0; 110} 111``` 112 113**Negative Example:** 114 115```ts 116@Sendable 117class A { 118 constructor() { 119 } 120 b: Array<number> = [1, 2, 3] // collections.Array must be used. 121} 122``` 123 124 125## The member variables of a sendable class or interface cannot use the exclamation mark (!) for assertion. 126 127**Positive Example:** 128 129```ts 130@Sendable 131class A { 132 constructor() { 133 } 134 a: number = 0; 135} 136``` 137 138**Negative Example:** 139 140```ts 141@Sendable 142class A { 143 constructor() { 144 } 145 a!: number; 146} 147``` 148 149 150## The member variables of a sendable class or interface do not support computed property names. 151 152**Positive Example:** 153 154```ts 155@Sendable 156class A { 157 num1: number = 1; 158 num2: number = 2; 159 add(): number { 160 return this.num1 + this.num2; 161 } 162} 163``` 164 165**Negative Example:** 166 167```ts 168enum B { 169 b1 = "bbb" 170} 171@Sendable 172class A { 173 ["aaa"]: number = 1; // ["aaa"] is allowed in other classes in ets files 174 [B.b1]: number = 2; // [B.b1] is allowed in other classes in ets files 175} 176``` 177 178 179## The template type of a sendable class, collections.Array, collections.Map, and collections.Set in the generic class must be Sendable. 180 181**Positive Example:** 182 183```ts 184import { collections } from '@kit.ArkTS'; 185 186try { 187 let arr1: collections.Array<number> = new collections.Array<number>(); 188 let num: number = 1; 189 arr1.push(num); 190} catch (e) { 191 console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`); 192} 193``` 194 195**Negative Example:** 196 197```ts 198import { collections } from '@kit.ArkTS'; 199 200try { 201 let arr1: collections.Array<Array<number>> = new collections.Array<Array<number>>(); 202 let arr2: Array<number> = new Array<number>(); 203 arr2.push(1); 204 arr1.push(arr2); 205} catch (e) { 206 console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`); 207} 208``` 209 210 211## Variables defined in the context of the current module cannot be used in a sendable class. 212 213Because the context of a sendable object varies among concurrent instances, direct access may cause unexpected behavior. A sendable object cannot use the variables defined in the context of the current module. Otherwise, a compile-time error is reported. 214 215> **Note** 216> 217> Since API version 12, a sendable class object of the top level can be used internally by the sendable class itself. 218 219**Positive Example:** 220 221```ts 222import { lang } from '@kit.ArkTS'; 223 224type ISendable = lang.ISendable; 225 226interface I extends ISendable {} 227 228@Sendable 229class B implements I { 230 static o: number = 1; 231 static bar(): B { 232 return new B(); 233 } 234} 235 236@Sendable 237class C { 238 v: I = new B(); 239 u: number = B.o; 240 241 foo() { 242 return B.bar(); 243 } 244} 245``` 246 247**Negative Example:** 248 249```ts 250import { lang } from '@kit.ArkTS'; 251 252type ISendable = lang.ISendable; 253 254interface I extends ISendable {} 255 256@Sendable 257class B implements I {} 258 259function bar(): B { 260 return new B(); 261} 262 263let b = new B(); 264 265{ 266 @Sendable 267 class A implements I {} 268 269 @Sendable 270 class C { 271 u: I = bar(); // bar is not a sendable class object. A compile-time error is reported. 272 v: I = new A(); // A is not defined in the top level. A compile-time error is reported. 273 274 foo() { 275 return b; // b is not a sendable class object but an instance of the sendable class. A compile-time error is reported. 276 } 277 } 278} 279``` 280 281 282## A sendable class and sendable function can only use the @Sendable decorator. 283 284If the class decorator is defined in a .ts file, any modification to the class layout causes a runtime error. 285 286**Positive Example:** 287 288```ts 289@Sendable 290class A { 291 num: number = 1; 292} 293``` 294 295**Negative Example:** 296 297```ts 298@Sendable 299@Observed 300class C { 301 num: number = 1; 302} 303``` 304 305 306## The Sendable type cannot be initialized using an object literal or array literal. 307 308A sendable data type can be created only by using the **new** expression of the Sendable type. 309 310**Positive Example:** 311 312```ts 313import { collections } from '@kit.ArkTS'; 314 315let arr1: collections.Array<number> = new collections.Array<number>(1, 2, 3); // The type is Sendable. 316``` 317 318**Negative Example:** 319 320```ts 321import { collections } from '@kit.ArkTS'; 322 323let arr2: collections.Array<number> = [1, 2, 3]; // The type is not Sendable. A compile-time error is reported. 324let arr3: number[] = [1, 2, 3]; // The type is not Sendable. No error is reported. 325let arr4: number[] = new collections.Array<number>(1, 2, 3); // A compile-time error is reported. 326``` 327 328 329## A non-sendable type cannot be converted to a sendable type using **as**. 330 331> **Note** 332> 333> A sendable type must be compatible with a non-sendable type without violating the sendable usage rules. Therefore, a sendable type can be converted to a non-sendable type using **as**. 334 335**Positive Example:** 336 337```ts 338class A { 339 state: number = 0; 340} 341 342@Sendable 343class SendableA { 344 state: number = 0; 345} 346 347let a1: A = new SendableA() as A; 348``` 349 350**Negative Example:** 351 352```ts 353class A { 354 state: number = 0; 355} 356 357@Sendable 358class SendableA { 359 state: number = 0; 360} 361 362let a2: SendableA = new A() as SendableA; 363``` 364 365 366## An arrow function is not sendable. 367 368An arrow function cannot be decorated by @Sendable. 369 370**Positive Example:** 371 372```ts 373@Sendable 374type SendableFuncType = () => void; 375 376@Sendable 377function SendableFunc() { 378 console.info("Sendable func"); 379} 380 381@Sendable 382class SendableClass { 383 constructor(f: SendableFuncType) { 384 this.func = f; 385 } 386 func: SendableFuncType; 387} 388 389let sendableClass = new SendableClass(SendableFunc); 390``` 391 392**Negative Example:** 393 394```ts 395@Sendable 396type SendableFuncType = () => void; 397let func: SendableFuncType = () => {}; // A compile-time error is reported. 398 399@Sendable 400class SendableClass { 401 func: SendableFuncType = () => {}; // A compile-time error is reported. 402} 403``` 404 405 406## The @Sendable decorator supports type decoration for functions only. 407 408The @Sendable decorator supports type decoration for functions only. 409 410**Positive Example:** 411 412```ts 413@Sendable 414type SendableFuncType = () => void; 415``` 416 417**Negative Example:** 418 419```ts 420@Sendable 421type A = number; // A compile-time error is reported. 422 423@Sendable 424class C {} 425 426@Sendable 427type D = C; // A compile-time error is reported. 428``` 429 430 431## Notice 432 433 434When using **Sendable** in HAR, enable the configuration of generating TS files. For details, see [Building TS Files](../quick-start/har-package.md#building-ts-files). 435 436 437## Rules for Interaction with TS/JS 438 439 440### ArkTS General Rules (Only for Sendable Objects Currently) 441 442| Rule Description| 443| -------- | 444| When a sendable object is passed to a TS/JS interface, the object layout cannot be operated (adding or deleting properties, or changing property types).| 445| When a sendable object is set to a TS/JS object, the object layout cannot be operated (adding or deleting properties, or changing property types) after the TS/JS object obtains the sendable object.| 446| When a sendable object is placed in a TS/JS container, the object layout cannot be operated (adding or deleting properties, or changing property types) after the TS/JS object obtains the sendable object.| 447 448> **Note** 449> 450> Changes of the property types do not include changes of the sendable object types, for example, from Sendable class A to Sendable class B. 451 452 453### Native API Rules (Only for Sendable Objects Currently) 454 455| Rule Description| 456| -------- | 457| Do not delete properties. The **napi_delete_property** interface cannot be used.| 458| Do not add properties. The following interfaces cannot be used: **napi_set_property**, **napi_set_named_property**, and **napi_define_properties**.| 459| Do not modify property types. The following interfaces cannot be used: **napi_set_property**, **napi_set_named_property**, and **napi_define_properties**.| 460| Symbol-related interfaces and types are not supported. The following interfaces cannot be used: **napi_create_symbol**, **napi_is_symbol_object**, and **napi_symbol**.| 461 462 463## Rules for Interaction with the UI 464 465The Sendable data needs to be used together with the [makeObserved](../quick-start/arkts-new-makeObserved.md) to observe the data changes of the Sendable object. For details, see [Using makeObserved Together with @Sendable Decorated Classes](../quick-start/arkts-new-makeObserved.md#using-makeobserved-together-with-sendable-decorated-classes). 466