Scala LanguageDynamic Invocation


Introduction

Scala allows you to use dynamic invocation when calling methods or accessing fields on an object. Instead of having this built deep into the language, this is accomplished through rewriting rules similar to those of implicit conversions, enabled by the marker trait [scala.Dynamic][Dynamic scaladoc]. This allows you to emulate the ability to dynamically add properties to objects present in dynamic languages, and more. [Dynamic scaladoc]: http://www.scala-lang.org/api/2.12.x/scala/Dynamic.html

Syntax

  • class Foo extends Dynamic
  • foo.field
  • foo.field = value
  • foo.method(args)
  • foo.method(namedArg = x, y)

Remarks

In order to declare subtypes of Dynamic, the language feature dynamics must be enabled, either by importing scala.language.dynamics or by the -language:dynamics compiler option. Users of this Dynamic who do not define their own subtypes do not need to enable this.