Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy's classX.metaClass.getProperty in scala

Is there anything equivalent to Groovy's remarkable SomeClass.metaClass.getProperty function in scala? This would be very helpful in making domain specific languages. For example, I could then say: val x = SomeClass(); x.arbitraryPropertyName rather than x.get("arbitraryPropertyName") or x("arbitraryPropertyName").

like image 737
vishvAs vAsuki Avatar asked Dec 06 '11 03:12

vishvAs vAsuki


1 Answers

From your description, this feature sounds like Scala's Dynamic. From the Scaladoc,

A marker trait that enables dynamic invocations. Instances x of this trait allow calls x.meth(args) for arbitrary method names meth and argument lists args. If a call is not natively supported by x, it is rewritten to x.applyDynamic("meth", args).

That is, if your class extends Dynamic, then calls of the form x.arbitraryPropertyName get translated to x.applyDynamic("arbitraryPropertyName"), and of course you get to define the behavior of applyDynamic.

like image 125
Kipton Barros Avatar answered Oct 07 '22 16:10

Kipton Barros