Skip to content

Commit 668633d

Browse files
KovshefulCodersarahhaggarty
authored andcommitted
Update K2 inc and dec operators example
Clearer comments to explain the differences between compiler versions, specifically why we can't call tau() in 2.0 after the inc() operator.
1 parent 1811c5a commit 668633d

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

docs/topics/k2-compiler-migration-guide.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,29 @@ fun main(input: Rho) {
311311
// Rho and Tau interfaces.
312312
if (unknownObject is Tau) {
313313

314-
// Use the overloaded inc() operator from interface Rho,
315-
// which smart-casts the type of unknownObject to Sigma.
314+
// Use the overloaded inc() operator from interface Rho.
315+
// In Kotlin 2.0.0, the type of unknownObject is smart-cast to
316+
// Sigma.
316317
++unknownObject
317318

318319
// In Kotlin 2.0.0, the compiler knows unknownObject has type
319320
// Sigma, so the sigma() function can be called successfully.
320321
unknownObject.sigma()
321322

322-
// In Kotlin 1.9.20, the compiler thinks unknownObject has type
323-
// Tau, so calling the sigma() function throws an error.
324-
323+
// In Kotlin 1.9.20, the compiler doesn't perform a smart cast
324+
// when inc() is called so the compiler still thinks that
325+
// unknownObject has type Tau. Calling the sigma() function
326+
// throws a compile-time error.
327+
325328
// In Kotlin 2.0.0, the compiler knows unknownObject has type
326-
// Sigma, so calling the tau() function throws an error.
329+
// Sigma, so calling the tau() function throws a compile-time
330+
// error.
327331
unknownObject.tau()
328332
// Unresolved reference 'tau'
329333

330-
// In Kotlin 1.9.20, the compiler mistakenly thinks that
331-
// unknownObject has type Tau, so the tau() function can be
332-
// called successfully.
334+
// In Kotlin 1.9.20, since the compiler mistakenly thinks that
335+
// unknownObject has type Tau, the tau() function can be called,
336+
// but it throws a ClassCastException.
333337
}
334338
}
335339
```
@@ -634,6 +638,8 @@ However, if you don't want to immediately initialize a property, you can:
634638
* Make the property `final`.
635639
* Use a private backing property that allows for deferred initialization.
636640

641+
For more information, see the [corresponding issue in YouTrack](https://youtrack.jetbrains.com/issue/KT-57555).
642+
637643
### Deprecated synthetics setter on a projected receiver
638644

639645
**What's changed?**

docs/topics/whatsnew20.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,25 +341,29 @@ fun main(input: Rho) {
341341
// Rho and Tau interfaces.
342342
if (unknownObject is Tau) {
343343

344-
// Uses the overloaded inc() operator from interface Rho,
345-
// which smart casts the type of unknownObject to Sigma.
344+
// Use the overloaded inc() operator from interface Rho.
345+
// In Kotlin 2.0.0, the type of unknownObject is smart-cast to
346+
// Sigma.
346347
++unknownObject
347348

348349
// In Kotlin 2.0.0, the compiler knows unknownObject has type
349350
// Sigma, so the sigma() function can be called successfully.
350351
unknownObject.sigma()
351352

352-
// In Kotlin 1.9.20, the compiler thinks unknownObject has type
353-
// Tau, so calling the sigma() function is not allowed.
354-
353+
// In Kotlin 1.9.20, the compiler doesn't perform a smart cast
354+
// when inc() is called so the compiler still thinks that
355+
// unknownObject has type Tau. Calling the sigma() function
356+
// throws a compile-time error.
357+
355358
// In Kotlin 2.0.0, the compiler knows unknownObject has type
356-
// Sigma, so calling the tau() function is not allowed.
359+
// Sigma, so calling the tau() function throws a compile-time
360+
// error.
357361
unknownObject.tau()
358362
// Unresolved reference 'tau'
359363

360-
// In Kotlin 1.9.20, the compiler mistakenly thinks that
361-
// unknownObject has type Tau, the tau() function can be
362-
// called successfully.
364+
// In Kotlin 1.9.20, since the compiler mistakenly thinks that
365+
// unknownObject has type Tau, the tau() function can be called,
366+
// but it throws a ClassCastException.
363367
}
364368
}
365369
```

0 commit comments

Comments
 (0)