• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scala dynamic types

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did anyone already use this? I just came across this feature which is available since Scala 2.10 and I cannot justify a valid usecase to use this.



That is just the syntax. I understand that I can call methods that are not defined, but I still would need to write that piece of logic that needs to happen when I call that method. I'm simply failing to understand where this piece of logic would be written and how does Dynamic help me call that?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I would have much need for this, given that one of the big advantages of Scala is supposed to be its static typing (otherwise why not use Clojure or Groovy?), but here's what Nilanjan Raychaudhuri says about this in Scala In Action:

Nilanjan Raychaudhuri wrote:Scala version 2.10 adds support for a Dynamic type. Using this feature you can dynamically add methods and fields to a type at runtime. This is very similar to the method_missing feature of Ruby and is quite useful if you’re building a domain-specific language (DSL). For example, Scala map is a collection of key value pairs and if you want to access the value associated with a key you can do something like the following:


Here someMap is a collection of two key value pairs and someMap.get("foo") will return 1. Using Dynamic we can easily change that so that we can access the keys as if they were part of a type:

The magic ingredient in this case is the selectDynamic method. (Scala methods are defined using the def keyword.) When the Scala compiler checks that foo is not part of the type it doesn’t give up immediately. If the type is a subtype of Dynamic it looks for the selectDynamic method and invokes it. If the method is not provided, you will get a compilation error.

 
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use dynamic types. In general they are used for interactions with an external (non-code) environment.

One use case is localization. See my simple messages library for an example. You may find usage examples under the test directory. This removes almost all boilerplate code used in the formatting.

Another set of use cases is data input from external formats. I have similar libraries for reading json, xml, SQL ResultSets, properties files (application configuration). Main purpose in the same as above: to reduce boilerplate code. I use code like this:

In that example there is a bunch of implicit and explicit conversions from DynamicTypeValue to any required type (Int, Long, String, Option[Something], Array[Something], etc...).

Same approach (dynamic with selectDynamic and set of conversions) is used for other mentioned readers like JSONInput.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic