This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Extend function with trait

1 reply
John Nilsson
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Hi,

Is it possible to extend a function with a trait inline?

Something like

trait MyTrait extends ((String) => String)

object A
{
  val f = (s:String) => {s} with MyTrait
}

BR,
John
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Extend function with trait
Hi John,

trait Monkey {}
val f = new Function1[String,String] with Monkey { override def apply(s : String) = s }

Perhaps not the pretties girl in town...
But if the trait had methods to be defined, how would it look in a similar syntax like this:

val f = (s:String) with Monkey => {} //Where to override/implement methods defined/declared in Monkey?

But I'm sure there's a workaround somewhere...

Have a nice holiday man, what happens at new years eve?

Cheers,
Viktor

On Mon, Dec 29, 2008 at 8:44 PM, John Nilsson <john [at] milsson [dot] nu> wrote:
Hi,

Is it possible to extend a function with a trait inline?

Something like

trait MyTrait extends ((String) => String)

object A
{
  val f = (s:String) => {s} with MyTrait
}

BR,
John



--
Viktor Klang
Senior Systems Analyst

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland