Playing with Scala

until now i withstood the forbidden fruit of spending time with scala. until this morning i just pushed this until Jan´s book was delivered ;)

but really, after i just had a first glance of it…. how could this be not interesting?

 Java |  copy |? 
object E {
  def main(args : Array[String]) : Unit = {
    new Foo().foo;
    (new Foo() with Bar).bar;
    (new Foo() with InterceptingFoo).foo;
  }
}
 
trait IFoo {
 def foo();
}
 
class Foo {
 def foo() { System.out.println("foo") }
}
 
trait Bar {
 def bar() { System.out.println("bar") }
}
 
trait InterceptingFoo extends IFoo {
 abstract override def foo() {
  System.out.println("before foo");
  super.foo();
  System.out.println("after foo");
 }
}

note that all of this magic is done at runtime! there is no class (well generated, but not written) that combines Foo and InterceptingFoo for instance.

NEAT!

 Java |  copy |? 
output:
foo
bar
before foo
foo
after foo

2 comments to Playing with Scala

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>