bon icon indicating copy to clipboard operation
bon copied to clipboard

support for default value for optional argument without custom setting

Open sehz opened this issue 7 months ago • 1 comments

Reference: https://github.com/elastio/bon/issues/231.

I have many use cases where to provide default value for optional argument. Reason for optional argument is that I want to provide reasonable default value but to disable if need to be. For example,

#[derive(Builder)]
pub struct MyGenerator {    
    #[builder(into, required, default = Some("default_world"))]
    default_export_world_name: Option<String>
}

sehz avatar Jun 16 '25 20:06 sehz

Hi! I may be misinterpreting the issue, but here is how I understand this. You want to have different behavior for when the setter is not called vs when you explicitly set None for it.

So for example, this should return a struct with default_export_world_name: Some("default_world")

MyGenerator::builder().build()

vs this should havedefault_export_world_name: None:

MyGenerator::builder().default_export_world_name(None).build()

The provided code example already generates the builder API like the above. However, it also creates a second method maybe_default_export_world_name(Option<Option<String>>) just so that you could dynamically pass either None to get the default value, or Some(None) to explicitly disable the default value and assign None.


The issue title mentions "without custom setting". I'm not sure how such behavior could be requested without an explicit attribute. Do you have any ideas of the desired syntax?

Veetaha avatar Jun 16 '25 20:06 Veetaha