Skip to content

Instantly share code, notes, and snippets.

@rjhall
Created December 18, 2012 20:21
Show Gist options
  • Save rjhall/4331591 to your computer and use it in GitHub Desktop.
Save rjhall/4331591 to your computer and use it in GitHub Desktop.
I am curious why class B has two methods called f, and why calling f returns a List rather than an Iterable.
scala> abstract class A { def f : Iterable[String] }
defined class A
scala> class B extends A { def f = List("foo", "bar") }
defined class B
scala> class C extends A { val r = math.random; def f = { val v = List("foo", "bar"); if(r < 0.5) v else v.map{_ + "_"} } }
defined class C
scala> (new B).f
res13: List[java.lang.String] = List(foo, bar)
scala> (new C).f
res14: Iterable[String] = List(foo, bar)
scala> (new C).f
res15: Iterable[String] = List(foo_, bar_)
scala> (new B).getClass.getMethods
res19: Array[java.lang.reflect.Method] = Array(public scala.collection.immutable.List B.f(), public scala.collection.Iterable B.f(), public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment