MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Dynamic Invocation

Examples

Field Accesses


This:

class Foo extends Dynamic {
 // Expressions are only rewritten to use Dynamic if they are not already valid
 // Therefore foo.realField will not use select/updateDynamic
 var realField: Int = 5
 // Called for expressions of the type foo.field
 def selectDynamic(fieldName: String) = ???
 def updateDynamic(fieldName: String)(value: Int) = ???
}

allows for simple access to fields:

val foo: Foo = ???
foo.realField // Does NOT use Dynamic; accesses the actual field
foo.realField = 10 // Actual field access here too
foo.unrealField // Becomes foo.selectDynamic(unrealField)
foo.field = 10 // Becomes foo.updateDynamic("field")(10)
foo.field = "10" // Does not compile; "10" is not an Int.
foo.x() // Does not compile; Foo does not define applyDynamic, which is used for methods.
foo.x.apply() // DOES compile, as Nothing is a subtype of () => Any
// Remember, the compiler is still doing static type checks, it just has one more way to
// "recover" and rewrite otherwise invalid code now.

Method Calls


This:

class Villain(val minions: Map[String, Minion]) extends Dynamic {
 def applyDynamic(name: String)(jobs: Task*) = jobs.foreach(minions(name).do)
 def applyDynamicNamed(name: String)(jobs: (String, Task)*) = jobs.foreach {
 // If a parameter does not have a name, and is simply given, the name passed as ""
 case ("", task) => minions(name).do(task)
 case (subsys, task) => minions(name).subsystems(subsys).do(task)
 }
}

allows for calls to methods, with and without named parameters:

val gru: Villain = ???
gru.blu() // Becomes gru.applyDynamic("blu")()
// Becomes gru.applyDynamicNamed("stu")(("fooer", ???), ("boomer", ???), ("", ???),
// ("computer breaker", ???), ("fooer", ???))
// Note how the `???` without a name is given the name ""
// Note how both occurrences of `fooer` are passed to the method
gru.stu(fooer = ???, boomer = ???, ???, `computer breaker` = ???, fooer = ???)
gru.ERR("a") // Somehow, scalac thinks "a" is not a Task, though it clearly is (it isn't)


Conclusion

In this page (written and validated by ) you learned about Scala Dynamic Invocation . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Enumerations.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.