Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook pattern in Scala

Tags:

scala

I'm looking for a concise documentation of all hooks in Scala. A hook is any situation in the program flow where the common behavior can be intercepted. Such situations include:

  • declaration of classes or traits
  • access to methods and fields
  • mix-in of threads, inheritance

I'm coming from a Ruby background where, for example, method_missing allows to intercept non-existent method calls.

Are such hooks available at all in Scala?

Matthias

like image 576
Matthias Guenther Avatar asked Mar 09 '11 07:03

Matthias Guenther


1 Answers

There is no equivalent of method_missing in Scala 2.8 or earlier. In Scala 2.9 (under development), a Dynamic trait will be added. Unknown method calls to objects declaring the Dynamic trait will be automatically translated by the compiler to instead call invokeDynamic. The idea is to get some of the power of dynamically typed languages in a safe and sane way, without paying the performance overhead of dynamic typing if unneeded. It also simplifies interoperability issues when calling objects defined in dynamic languages from within Scala.

Other than that, hooking in new behaviour in Scala is largely done either via classic inheritance, or by addiing new functionality to objects via implicit conversions.

like image 149
Dave Griffith Avatar answered Sep 30 '22 20:09

Dave Griffith