Why does Scala List[Int].contains accept Option[Int]? -


so given list:

val a:list[int] = list(1,2,3) 

you can do:

a.contains(option(2)) 

which returns false. understand following function definitions allowing compile @ done on purpose:

def contains[a1 >: a](elem : a1) : scala.boolean = {...} sealed abstract class option[+a]() ... 

what don't understand why - there use case useful? if automatically flattened option before comparison above returned true might useful return false.

i ask because it's easy forget unwrap option variable pass list.contains, leading potentially difficult find bugs.

thanks!

imagine following:

sealed trait result case class simpleresult(x: int) extends result case class fancyresult(x: int, y: int) extends result  val okresults: list[simpleresult] = // ...  def calcresult: result = ???  okresults.contains(calcresult) 

this case useful can call contains supertype of type of list.

in case, nasty side-effect happens: option[int] not supertype of int. however, have common supertype: any. call contains gets inferred to:

a.contains[any](option(2)) 

since option[int] subtype of any, allowed pass contains. wouldn't want in cases, hard (impossible?) define such interface in scala's type system.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -