Repository navigation
Is it possible to statically check a value_parser type against an argument value type? #6474
Replies: 3 comments 3 replies
|
Short answer: no, not currently, and this is a known open limitation rather than something you are doing wrong. Why it is not caught at compile timeIn the derive API, There is a long-standing issue tracking exactly what you are asking for: #4830 "better compile time validation of value_parser types". The maintainer response there acknowledges the failure happens in The underlying reason is that The workaround:
|
|
You are right, and mine was actively worse than doing nothing. I compiled all the variants to be sure rather than guess, results below. Why yours checks and mine does not
Your version, mismatched types: #[clap(long, value_parser = ZeroF64.map(|v: usize| v))]
n: usize,Exactly the static check you asked for, and the diagnostic names both types. My version: #[clap(long, value_parser = ZeroF64.map(|v| v as usize))]
n: usize,Compiles clean. The input stays inferred as A self-documenting variant, also verifieduse std::convert::identity;
#[clap(long, value_parser = ZeroF64.map(identity::<usize>))]
n: usize,Same constraint, and the name states the intent: assert a type, convert nothing. Function pointers satisfy Confirmed it does not false positive on correct usage either: #[clap(long, value_parser = ZeroF64.map(identity::<f64>))]
n: f64,Compiles and parses normally ( Full test matrix
Tested on rustc 1.75.0, clap 4.4.18. Answering the original questionYes, it is achievable today, with the boundary you already identified. The idiom catches:
It does not catch a On
|
|
Yes, this would be desired. The problem is that we have to Looking, I'm not seeing a direct issue for this. #3864 covers a special case for this that may or may not be solved the same way. #4830 is for the builder API and not the derive API. |
Uh oh!
There was an error while loading. Please reload this page.
As a trivial example:
A compile-time error for the mismatch between
f64andusizewould be desirable.All reactions