Skip to content

Conversation

@colin-kiegel
Copy link
Contributor

Follow-up on #30 to implement #10. This is the proposed documentation of the feature:

You can use #{...} for some basic computations inside of quotations. Computations are
restricted to combinations of:

  • #{x[0]} - index arrays with integers 0..32
  • #{x.0} - index tuple structs with integers 0..32
  • #{x.foo} - access fields
  • #{x()} - call functions (without arguments)

Note:

  • Any chained combination of the above is also possible, like #{self.foo[0].bar()}.
    But please consider replacing too complex computations with helper variables #bar
    in order to improve the readability for other people - thank you in advance. ;-)
  • These computations can be particularly useful if you want to implement quote::ToTokens
    for a custom struct. You can then reference the struct fields directly via #{self.foo}
    etc.
  • computations #{...} inside of repetitions #(...)* are treated as constant expressions
    and are not iterated over - in contrast to any other #x inside of a repetition.

I have added some compiletests which assert that more powerful computations are indeed rejected.

The restriction is isolated in a little helper macro here.

- implement #10
- restrict to array/tuple/struct access and function calls
@colin-kiegel
Copy link
Contributor Author

My primary use case for this would be implementing quote::ToTokens for a struct like here.

@dtolnay are you ok with these restrictions, or do you think it should be restricted even further?

@dtolnay
Copy link
Owner

dtolnay commented Apr 26, 2017

Thanks, the implementation looks good but I don't plan to accept a computation feature. Maybe publish a fork that provides this?

@jhpratt
Copy link

jhpratt commented Jan 8, 2020

@dtolnay Are your opinions the same as they were ~3 years ago?

@dtolnay
Copy link
Owner

dtolnay commented Jan 8, 2020

Yes they are.

Repository owner locked and limited conversation to collaborators Jan 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants