anp / moxie

lightweight platform-agnostic tools for declarative UI
https://moxie.rs
Apache License 2.0
827 stars 27 forks source link

mox: How to invoke nullary functions (functions with 0 arguments)? #276

Open cakekindel opened 3 years ago

cakekindel commented 3 years ago

hi! i'm working through adding mox builder support to my slack-blocks data structure library and can't find a way to call a nullary function; if i had:

struct Foo {
  bar: bool,
}

impl Foo {
  pub fn build() -> FooBuilder {
    FooBuilder { bar: false }
  }
}

struct FooBuilder {
  bar: bool
}

impl FooBuilder {
  pub fn bar(mut self) -> Self {
    self.bar = true;
    self
  }
}

My expectation is that there would be some syntax to invoke bar:

mox! {
  <Foo::build bar />
  <Foo::build bar={()} />
  <Foo::build bar=_ />
  <Foo::build bar() />
}
anp commented 3 years ago

Good point!

Of your proposed options, I think I might the last one the most. The first conflicts with punned attribute/binding names, so I don't it would work. The second is going to be painful for users and would conflict with attributes that accept a single () argument (the curly braces could just be a block). The third might work but I'm not sure whether you can actually reference a binding named _ or whether it's permanently unbound.

One other perk of the fourth option (<Foo::build bar() />) is that it might work for attributes with more than one argument? You might have a builder which takes two arguments in addition to the receiver, and this would probably make it clear what's happening.

It might be tricky to implement with the current parser, not sure. Feel free to give it a shot!

cakekindel commented 3 years ago

sweet! I'll dig around and try to come up with a proposed implementation soon.