Traits in `#[derive(...)]` don't accept arguments

I want to add some arguments in my derive macro, but if I wrote this:

#[derive(MyTest(1, true))]
struct Foo {}

The compiler errored my with this: error: traits in #[derive(...)] don't accept arguments.

I think this is because the syntax is not supported by design, but I'm curious where are the "traits" come from? And any practice way to add arguments in a derive macro?

Basically, derive macros were designed for the single purpose: to generate implementation for some traits automatically, based on the struct definition. So in your code MyTest is expected to be a trait.

Probably the best way would be to add custom attributes on struct, which the derive macro will read and use. That's what derivative uses, for example.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.