Unexpected Behavior with Option Mapping in Scala 2.13 and Custom Types
I'm working with Scala 2.13 and trying to use the `map` function on an `Option` that contains a custom case class, but I'm running into unexpected behavior... The goal is to transform the inner value of the `Option` and return a new `Option` of a different type. Hereβs a simplified version of my code: ```scala case class User(name: String, age: Int) val userOption: Option[User] = Some(User("Alice", 25)) val userName: Option[String] = userOption.map(user => user.name) println(userName) ``` This works as expected and outputs `Some(Alice)`. However, when I try to do something more complex, like this: ```scala case class User(name: String, age: Int) case class UserProfile(user: User, bio: String) val userOption: Option[User] = Some(User("Alice", 25)) val userProfile: Option[UserProfile] = userOption.map(user => UserProfile(user, "Hello, I'm Alice!")) println(userProfile) ``` I get the output `Some(UserProfile(User(Alice,25),Hello, I'm Alice!))`, which is fine. The problem arises when `userOption` is `None`: ```scala val userOption: Option[User] = None val userProfile: Option[UserProfile] = userOption.map(user => UserProfile(user, "Hello, I'm Alice!")) println(userProfile) ``` I expect this to output `None`, which it does, but in addition, I'm seeing a warning about "possible non-exhaustive pattern match" if I later try to pattern match on `userProfile` like this: ```scala userProfile match { case Some(profile) => println(profile.bio) case None => println("No user profile found.") } ``` Is there a way to avoid this warning? It seems like the compiler is not able to infer that `userProfile` will definitely be `None` when `userOption` is `None`, even though the logic is sound. I've tried adding type annotations and ensuring my case classes donβt have any additional complexities, but the warning persists. Any insights on how to handle this situation gracefully? This is part of a larger API I'm building.