« Return to Thread: [scala] Constructor parameters for traits

Re: [scala] Constructor parameters for traits

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View in Thread

On Tue, Jun 30, 2009 at 06:57:30AM -0700, Andrew McCallum wrote:
> I'm desperate for trait constructor arguments only because I need a
> Manifest for my trait's type argument (in order to include code whose
> behavior depends on the type). Something like:
>
> trait Farm[A <: Animal](implicit m:Manifest[A]) {
>   def foo(a:Animal) = if (m.erasure == a.getClass) bar1 else bar2
> }

scala> trait Farm[A <: AnyRef] { def m(implicit man: reflect.Manifest[A]) = man }

scala> (new Farm[String] {}).m
res3: scala.reflect.Manifest[String] = java.lang.String

scala> (new Farm[List[String]] {}).m
res4: scala.reflect.Manifest[List[String]] = scala.collection.immutable.List[java.lang.String]

(Not sure what those Strings are doing on the Farm, but anyway.)

--
Paul Phillips      | If this is raisin, make toast with it.
Future Perfect     |
Empiricist         |
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------

 « Return to Thread: [scala] Constructor parameters for traits