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

Re: [scala] Constructor parameters for traits

by Miles Sabin :: Rate this Message:

Reply to Author | View in Thread

On Tue, Jun 30, 2009 at 2:57 PM, Andrew McCallum<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
> }
>
> Is there some other recommended work-around?  Of course my true use-case is
> a bit more complicated.  I'm stuck without a solution.

Couldn't you move the implicit parameter to the method?

trait Farm[A <: Animal] {
 def foo(a:Animal)(implicit m:Manifest[A]) = if (m.erasure ==
a.getClass) bar1 else bar2
}

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin

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