Skip to content

Commit

Permalink
Add explanation for code
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Oct 5, 2024
1 parent e817eb1 commit ca7553e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/init-global/warn/Color.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ enum Color:
enum Player:
case Black, White

// Explanation: See the desugaring below
val color: Color =
if this == Player.Black // warn
then Color.Black
else Color.White

// From the desugaring of Player, we can see the field `Player.Black` is not yet
// initialized during evaluation of the first `new Player`:
//
// class Player:
// val color: Color =
// if this == Player.Black ...
//
// object Player:
// val Black: Player = new Player // <--- problem
// val White: Player = new Player
//
//
// The complex desugaring makes it difficult to see the initialization
// semantics and it is prone to make such hard-to-spot mistakes.
//
// Note: The desugaring above is simplified for presentation.

0 comments on commit ca7553e

Please sign in to comment.