Skip to content

Is there a way to declare builder only fields? #285

Answered by Veetaha
inflation asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, if you'd like to have custom fields only during the building process, then consider using a function-based builder instead of derive(Builder):

struct Example {
     x: u32,
     y: u32,
}

#[bon::bon]
impl Example {
    #[builder]
    fn new(
        x: u32,
        y: u32,
        custom_builder_only_field: &u32,
    ) -> Self {
        // ... use the `custom_builder_only_field` here to build the struct
        
        // Return the final struct
        Self { x, y }
    }
}

Example::builder()
    .x(1)
    .y(2)
    .custom_builder_only_field(&3)
    .build();

Or I may have misunderstood your use case. Could you share some specific example of code to better demonstrate the problem?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Veetaha
Comment options

Answer selected by inflation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants