Scala type input question

I just fiddled with Tony Morris's excellent catamorphism exercise when I reflected on what was happening in the following situation ..

def cata[X](some: A => X, none: => X): X 

Let me now name this method as follows:

 def isDefined: Boolean = cata( _ => true, false) 

I was wondering if the inferencer type defines the type _ => true as A => Boolean or Any => Boolean . Due to the fact that Function1 is contravariant in its input parameter, both of the following compile just fine:

 def isDefined: Boolean = cata( (_: A) => true, false) //#1 def isDefined: Boolean = cata( (_: Any) => true, false) //#2 

So the question is, is the inferencer type suitable for # 1 or # 2?

+6
scala type-inference catamorphism
source share
1 answer

I tried this:

 trait MyOption[+A] { def cata[X](some: A => X, none: => X): X def isDefined: Boolean = cata( _ => true, false) } 

and compiled it with scalac -Xprint:types . This gave the following result:

 [[syntax trees at end of typer]]// Scala source: myoption.scala package { abstract trait MyOption[A >: Nothing : Nothing X, none: => X): X; def isDefined: Boolean = MyOption.this.cata[Boolean](((x$1: A) => true), false) } } 

So, in appearance, the inferencer type came up with option # 1.

+7
source share

All Articles