Syntatic Sugar for Option mappings

it could be implemented using this proposition PRE SIP: ThisFunction | scope injection (similar to kotlin receiver function) (form of this PRESIP will be changed but main idea is there)

implicit class OptionsSuperOps[T](d:Option[T]) {
  def ?[E] (with this c:T => E) = { d.map(c) }
  //def >[E] (with this c:T => E) = { d.map(c) }
  def ??[E] (with this c:T => Option[E]) = { d.flatMap(c) }
  //def >>[E] (with this c:T => Option[E]) = { d.flatMap(c) }
}

User(
  userId = Some(id),
  firstName = name ? first,
  middleName = name ?? middle,
  lastName = name ?? last,
)

//User(
//  userId = Some(id),
//  firstName = name > first,
//  middleName = name >> middle,
//  lastName = name >> last,
//)

but there is rather small chance that this feature’ll land in scala (and no chance that it’ll land in this form).

1 Like