Design
new { def ...} creates a new anonymous object with the structural type AnyRef{def ...} :
scala> val aa = new { def kk = "bb" } aa: AnyRef{def kk: String}
Your UNTIL method is available because of a function called "reflective member access of a structural type", and you should also have import scala.language.reflectiveCalls , at least in Scala 2.11.2 as a result of SIP 18: Modular language functions :
scala> aa.kk <console>:9: warning: reflective access of structural type member method kk should be enabled by making the implicit value scala.language.reflectiveCalls visible. This can be achieved by adding the import clause 'import scala.language.reflectiveCalls' or by setting the compiler option -language:reflectiveCalls. See the Scala docs for value scala.language.reflectiveCalls for a discussion why the feature should be explicitly enabled. aa.kk ^ res0: String = bb
Note that this is a bit slower than defining class Repeatable {def UNTIL = ...} , because (for the JVM) the REPEAT function simply returns Object (AnyRef), and there is no type to drop from it, so Scala calls UNTIL using reflection. He also did not introduce some synthetic class, because the structural type can match any existing class (any other class with the corresponding UNTIL method).
source share