google / google/argh

[Breaking change] Accept raw expression for default values

Open
#74 1 comment 4 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2k
Forks
102
PR merge metrics
No merged PRs in 30d

Description

There might be a reason why `default` requires an expression-as-a-string, but I can't think of one. The [reference](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros) has examples of macros doing lots of funky things inside attributes.

It would be nice if someone could write, for example,

```rust
#[derive(FromArgs)]
struct GoUp {
#[argh(option, default = default_height())]
height: usize,

#[argh(option, default = String::from("only up"))]
direction: String,
}
```

The second example in particular is much nicer than what we have to write today,

```rust
#[argh(option, default = "String::from(\"only up\")")]
direction: String,
```

We could even wrap the expression with `Into::into()` in the expansion so it gets even shorter..

```rust
#[argh(option, default = "only up")]
direction: String,
```

The only problem is that this is definitely a breaking change.

One possible soft migration is to add a new attribute `default_expr` which has these semantics.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.