I have a domain class like:
class MyDomainClass{
String name
}
And an interface with a signature like:
BigDecimal doBigThangs(MyDomainClass startHere)
I want to be able to call it like this
doBigThangs('stuff')
And have it automatically coerse the string ‘stuff’ into the appropriate MyDomainClass. This is what I have tried, but perhaps I need to use “asType” or something.
ExpandoMetaClass.enableGlobally()
String.metaClass.toMyDomainClass = {->MyDomainClass.findByNameLike(delegate)}
You are correct: you can add a type conversion by overriding
asType. Your example would look something like this:However, groovy won’t silently cast a String to another type on a method call. You’ll have to call your method like this: