
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - Dynamic Invocation
This chapter takes you through the concept of dynamic invocation in Scala programming. Dynamic invocation is that you can call methods on objects dynamically at runtime. It is used when you work with dynamic languages and frameworks that require runtime method resolution.
Dynamic Invocation
You can invoke methods on objects whose types are not known until runtime. This is used when you interact with dynamic languages, scripting languages, APIs, etc. These provide objects with methods not known at compile-time.
Dynamic invocation can be achieved using Dynamic trait. So, objects act like dynamic language objects. It provides method calls and property accesses that are resolved at runtime, rather than statically by the compiler.
Syntax
The syntax for dynamic invocation in Scala using the Dynamic trait -
import scala.language.dynamics class DynamicExample extends Dynamic { def applyDynamic(name: String)(args: Any*): String = { s"Called method '$name' with arguments ${args.mkString(", ")}" } } val example = new DynamicExample println(example.someMethod(1, 2, 3))
Example of Dynamic Invocation
The following example shows defining and using dynamic invocation in Scala programming -
import scala.language.dynamics class DynamicExample extends Dynamic { def applyDynamic(name: String)(args: Any*): String = { s"Called method '$name' with arguments ${args.mkString(", ")}" } } object Demo { def main(args: Array[String]): Unit = { val example = new DynamicExample println(example.someMethod(1, 2, 3)) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Called method 'someMethod' with arguments 1, 2, 3
In the example, the class DynamicExample extends the Dynamic trait. It has dynamic method invocation. The applyDynamic method is used for method calls dynamically.
Advantages of Dynamic Invocation
There are various advantages of Dynamic invocation. It provides flexibility because method calls are resolved at runtime. You can integrate dynamic languages and frameworks. You can also reduce boilerplate code when working with repetitive method calls and when the method names are not known until runtime.
Dynamic Invocation with Reflection
You can achieve dynamic invocation using reflection too. It inspects and invokes methods on objects at runtime.
Syntax
The syntax of using reflection for dynamic invocation is -
import scala.reflect.runtime.universe._ def invokeMethod(obj: Any, methodName: String, args: Any*): Any = { val runtimeMirror = runtimeMirror(obj.getClass.getClassLoader) val instanceMirror = runtimeMirror.reflect(obj) val methodSymbol = runtimeMirror.reflectClass(instanceMirror.symbol.asClass).symbol. typeSignature.member(TermName(methodName)).asMethod instanceMirror.reflectMethod(methodSymbol)(args: _*) }
Example
Consider the example of using reflection for dynamic invocation in Scala programming -
class Example { def greet(name: String): String = s"Hello, $name!" } object Demo { import scala.reflect.runtime.universe._ def invokeMethod(obj: Any, methodName: String, args: Any*): Any = { val runtimeMirror = runtimeMirror(obj.getClass.getClassLoader) val instanceMirror = runtimeMirror.reflect(obj) val methodSymbol = runtimeMirror.reflectClass(instanceMirror.symbol.asClass). symbol.typeSignature.member(TermName(methodName)).asMethod instanceMirror.reflectMethod(methodSymbol)(args: _*) } def main(args: Array[String]): Unit = { val example = new Example println(invokeMethod(example, "greet", "Scala")) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Hello, Scala!
In the example, the invokeMethod function uses reflection to dynamically. It invokes the greet method on Example object.
Dynamic Invocation in Scripting
Dynamic invocation is used in scripting environments where the method names and parameters are determined at runtime.
Syntax
The syntax of dynamic invocation in scripting contexts is similar to the previous examples. It uses either the Dynamic trait or reflection.
Example
Consider the example of using dynamic invocation in a scripting context in Scala programming -
import scala.language.dynamics class Scriptable extends Dynamic { def applyDynamic(name: String)(args: Any*): String = { s"Executing script method '$name' with arguments ${args.mkString(", ")}" } } object Demo { def main(args: Array[String]): Unit = { val script = new Scriptable println(script.runScript("arg1", "arg2")) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Executing script method 'runScript' with arguments arg1, arg2
In the example, the class Scriptable uses dynamic invocation. It simulates executing script methods with arbitrary names and arguments.
Dynamic Invocation with Dynamic Traits
Scala has dynamic traits to work with dynamic method invocation. You can extend these traits to create classes that support dynamic method calls.
Syntax
The syntax of using dynamic traits for dynamic invocation is -
import scala.language.dynamics class DynamicExample extends Dynamic { def applyDynamic(name: String)(args: Any*): String = { s"Called method '$name' with arguments ${args.mkString(", ")}" } }
Example
Consider the example of using dynamic traits for dynamic invocation in Scala programming -
import scala.language.dynamics class DynamicExample extends Dynamic { def applyDynamic(name: String)(args: Any*): String = { s"Called method '$name' with arguments ${args.mkString(", ")}" } } object Demo { def main(args: Array[String]): Unit = { val example = new DynamicExample println(example.someMethod(1, 2, 3)) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Called method 'someMethod' with arguments 1, 2, 3
In the example, the class DynamicExample extends Dynamic trait. So dynamic method invocation using the applyDynamic method.
Dynamic Invocation Summary
- You can invoke methods on objects dynamically at runtime. So it provides flexibility and integration with dynamic languages and frameworks.
- You can achieve dynamic invocation using Dynamic trait and reflection. So there can be method calls on objects whose types are not known until runtime.
- Dynamic invocation enhances code flexibility, reduces boilerplate code, and integrates with dynamic languages and APIs.
- Dynamic invocation can be used in various contexts, like scripting environments and dynamic trait implementations.