0

Consider this piece of code

def foo(b: Int) = ???

val a = 3
foo(a)

It works fine of course, but let's say that later I decided that foo should support an empty input, so I change it's signature to

def foo(b: Option[Int]) = ???

Now foo(a) won't compile, because a isn't an Option[Int], it's an Int. So you have to say a = Some(3) or foo(Some(a)), which seems redundant to me.

My question is, why doesn't the language have an implicit conversion from T to Some[T] (and vice versa)?

Is it a good idea to implement one yourself?

Dotan
  • 6,602
  • 10
  • 34
  • 47
  • I don't really see, why you think it is redundant. – Thomas Böhm Dec 09 '17 at 11:13
  • 1
    No, it is a bad idea to introduce such an implicit. It would make the language less strongly typed. implicit conversions are kind of power which requires responsibility (mostly for DSLs). – Gábor Bakos Dec 09 '17 at 11:39
  • 9
    Possible duplicate of [Could/should an implicit conversion from T to Option\[T\] be added/created in Scala?](https://stackoverflow.com/questions/1746359/could-should-an-implicit-conversion-from-t-to-optiont-be-added-created-in-scal) – danielnixon Dec 09 '17 at 11:40
  • 2
    Also https://stackoverflow.com/questions/27590756/scala-option-implicit-conversion-bad-practice-or-missing-feature – danielnixon Dec 09 '17 at 11:41

0 Answers0