Skip to content

Support defaultable types for adding table rows #442

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

Closed
molpopgen opened this issue Jan 12, 2023 · 2 comments
Closed

Support defaultable types for adding table rows #442

molpopgen opened this issue Jan 12, 2023 · 2 comments
Labels
API API. issues enhancement New feature or request

Comments

@molpopgen
Copy link
Member

molpopgen commented Jan 12, 2023

The following pseudo code shows how struct update syntax can be used to provide API sugar w/o default arguments:

#[derive(Copy, Clone, Debug, Hash)]
struct NodeDefaults {
    flags: i32,
    population: i32,
    individual: i32,
}

impl Default for NodeDefaults {
    fn default() -> Self {
        Self {
            flags: 0,
            population: -1,
            individual: -1,
        }
    }
}

fn add_node_with_defaults(_time: f64, _defaults: NodeDefaults) {}

fn main() {
    add_node_with_defaults(0.0, NodeDefaults::default());

    let defaults = NodeDefaults {
        population: 2,
        ..Default::default()
    };
    assert_eq!(defaults.population, 2);
    assert_eq!(defaults.individual, -1);
    add_node_with_defaults(0.0, defaults);
    // Type is Copy, so we can re-use
    add_node_with_defaults(0.0, defaults);

    let defaults2 = NodeDefaults {
        individual: 2,
        ..defaults
    };
    assert_eq!(defaults2.population, 2);
    assert_eq!(defaults2.individual, 2);
    add_node_with_defaults(0.0, defaults2);
}

See this blog post for discussion.

@molpopgen
Copy link
Member Author

#466 implemented this for node table rows.

@molpopgen
Copy link
Member Author

closed by #448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API. issues enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant