You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Erwin Müller <er...@deventm.org> on 2016/04/19 14:50:50 UTC
Weird behavior with .with
Hello,
I'm trying to construct a DSL, but I don't like to write a builder, I want to
use just the Groovy syntax. But I have now a weird behavior of a script. I try
to explain in the source code.
----
profile.with { // Profile class
hostname with { } // ProfileProperties class // 1.
hosts() with { } // 2.
remote.with { service = 'openssh' } // 3.
mail with { // 4.
service = 'postfix'
delivery = 'courier'
}
}
----
----
class Profile {
Object methodMissing(String name, Object args) {
p = new ProfileProperties()
list.add(p);
return p
}
Object propertyMissing(String name, Object args) {
p = new ProfileProperties()
list.add(p);
return p
}
}
----
----
class ProfileProperties {
Object propertyMissing(String name, Object args) {
map.put(name, args);
}
}
----
1. here, the method Profile#methodMissing() is called with name="hostname" and
args=[null], that returns ProfileProperties
2. here, the method Profile#methodMissing() is called with name="hostname" and
args=[], that returns ProfileProperties
3. a) here, the method Profile#propertyMissing() is called with name="hostname"
and args=null, that returns ProfileProperties.
3. b) Later, the method ProfileProperties#propertyMissing() is called with
name="service" and args="openssh"
4. a) Here, the method Profile#propertyMissing() is called with name="service"
and args="postfix". *That is strange.* Why is first the property called?
4. b) Later, the method Profile#methodMissing() is called with name="mail" and
args=[courier], that returns ProfileProperties. *That is strange.* Why is the
argument "courier"?
Why is in case 4. first the properties "service" and "delivery" called, and not
the method "mail"?
I would have expected the behavior that first the method "mail()" is called,
that invokes the Profile#methodMissing() method that would create and return
the ProfileProperties object. Then later, the "service" and "delivery"
properties are intercepted by the ProfileProperties#propertyMissing() method.
Kind regards,
Erwin.
--
Erwin Müller - erwin.mueller@deventm.org
Software Entwickler - (+49) 01577-9505569
Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
Meine Seite - http://www.mueller-public.de/
ANRI - http://www.anr-institute.com/
Re: Weird behavior with .with
Posted by Dinko Srkoč <di...@gmail.com>.
mail with { // 4.
service = 'postfix'
delivery = 'courier'
}
is actually resolved as
this.mail(this.with({
service = 'postfix'
delivery = 'courier'
}))
where `this` is the instance of `Profile`.
BTW, the same happens with `hostname with {}`, but, because of the
empty block, you didn't notice it.
Cheers,
Dinko
On 19 April 2016 at 14:50, Erwin Müller <er...@deventm.org> wrote:
> Hello,
>
> I'm trying to construct a DSL, but I don't like to write a builder, I want to
> use just the Groovy syntax. But I have now a weird behavior of a script. I try
> to explain in the source code.
>
> ----
> profile.with { // Profile class
> hostname with { } // ProfileProperties class // 1.
> hosts() with { } // 2.
> remote.with { service = 'openssh' } // 3.
> mail with { // 4.
> service = 'postfix'
> delivery = 'courier'
> }
> }
> ----
> ----
> class Profile {
>
> Object methodMissing(String name, Object args) {
> p = new ProfileProperties()
> list.add(p);
> return p
> }
>
> Object propertyMissing(String name, Object args) {
> p = new ProfileProperties()
> list.add(p);
> return p
> }
> }
> ----
> ----
> class ProfileProperties {
>
> Object propertyMissing(String name, Object args) {
> map.put(name, args);
> }
> }
> ----
>
> 1. here, the method Profile#methodMissing() is called with name="hostname" and
> args=[null], that returns ProfileProperties
>
> 2. here, the method Profile#methodMissing() is called with name="hostname" and
> args=[], that returns ProfileProperties
>
> 3. a) here, the method Profile#propertyMissing() is called with name="hostname"
> and args=null, that returns ProfileProperties.
>
> 3. b) Later, the method ProfileProperties#propertyMissing() is called with
> name="service" and args="openssh"
>
> 4. a) Here, the method Profile#propertyMissing() is called with name="service"
> and args="postfix". *That is strange.* Why is first the property called?
>
> 4. b) Later, the method Profile#methodMissing() is called with name="mail" and
> args=[courier], that returns ProfileProperties. *That is strange.* Why is the
> argument "courier"?
>
> Why is in case 4. first the properties "service" and "delivery" called, and not
> the method "mail"?
>
> I would have expected the behavior that first the method "mail()" is called,
> that invokes the Profile#methodMissing() method that would create and return
> the ProfileProperties object. Then later, the "service" and "delivery"
> properties are intercepted by the ProfileProperties#propertyMissing() method.
>
> Kind regards,
>
> Erwin.
>
> --
> Erwin Müller - erwin.mueller@deventm.org
> Software Entwickler - (+49) 01577-9505569
> Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
> Meine Seite - http://www.mueller-public.de/
> ANRI - http://www.anr-institute.com/