How to resolve the algorithm Send an unknown method call step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Send an unknown method call step by step in the Scala programming language
Table of Contents
Problem Statement
Invoke an object method where the name of the method to be invoked can be generated at run time.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Send an unknown method call step by step in the Scala programming language
Source code in the scala programming language
class Example {
def foo(x: Int): Int = 42 + x
}
object Main extends App {
val example = new Example
val meth = example.getClass.getMethod("foo", classOf[Int])
assert(meth.invoke(example, 5.asInstanceOf[AnyRef]) == 47.asInstanceOf[AnyRef], "Not confirm expectation.")
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the C# programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the C programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Elm programming language
You may also check:How to resolve the algorithm Test a function step by step in the J programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Erlang programming language