SAM closures are a way to create an implementation for an interface with a single method easily.
In Java, creation of such interfaces requires the use of annonymous classes.
In Scala, we have functions.
But first, why create a single method interface if you can use a function? Well, there are two reasons I can think of:
- Typing: if a method accepts an argument of type SAM, then it is is easy to see who extends this type with concrete implementations.
- It allows to add more methods to the interface later on. Sometimes a new feature requires adding a method (maybe overloading) to the interface
Here is how one can simulate SAM closures in Scala:
In SAM.scala