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")
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With