Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cakekindel opened this issue May 27, 2021 · 2 comments · May be fixed by #277
Open

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

cakekindel opened this issue May 27, 2021 · 2 comments · May be fixed by #277

Comments

@cakekindel
Copy link

cakekindel commented May 27, 2021

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
Copy link
Owner

anp commented May 27, 2021

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
Copy link
Author

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

@cakekindel cakekindel linked a pull request Jun 1, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants