I'm trying to come up with consistent naming and ran into this issue:
/// Provides the forecaster with ability to calculate taxes.
pub trait TaxCalculator {
/// Calculate the taxes.
///
/// * _return_ - Calculated taxes.
fn calculate_taxes(
& mut self
) -> f64;
}
////////////////////////////////////////////////////////////////////////////////////
// --- structs ---
////////////////////////////////////////////////////////////////////////////////////
/// A simulator
pub struct Simulator<TaxCalculator>
where
TaxCalculator: TaxCalculator + Send + Sync + Clone {
/// A tax calculator.
pub tax_calculator: TaxCalculator
}
This fails with the following error because the type parameter TaxCalculator
is the same as the trait name.
Compiling playground v0.0.1 (/playground)
error[E0404]: expected trait, found type parameter `TaxCalculator`
--> src/lib.rs:17:20
|
2 | / pub trait TaxCalculator {
3 | | /// Calculate the taxes.
4 | | ///
5 | | /// * _return_ - Calculated taxes.
... |
8 | | ) -> f64;
9 | | }
| |_- you might have meant to refer to this trait
...
15 | pub struct Simulator<TaxCalculator>
| ------------- found this type parameter
16 | where
17 | TaxCalculator: TaxCalculator + Send + Sync + Clone {
| ^^^^^^^^^^^^^ not a trait
For more information about this error, try `rustc --explain E0404`.
I can address by either:
- Changing the name of the trait to something like
TaxCalculatorTrait
- Change the name or casing of the type parameter - e.g. T, TAX_CALCULATOR, TaxCalculatorImpl
- Introducing a new name? Maybe my thought process is not great and I'm doing it wrong.
I feel like my naming is reasonable. I thought a trait was an abstraction characterized by a set of functions. In this case there will be many types of tax calculators that will implement the trait.
From The Rust Programming Language
To parameterize the types in a new single function, we need to name the type parameter, just as
we do for the value parameters to a function. You can use any identifier as a type parameter name.
But we’ll useT
because, by convention, type parameter names in Rust are short, often just a letter,
and Rust’s type-naming convention is CamelCase. Short for “type,”T
is the default choice of most
Rust programmers.
I can see how short single letter type names work for very common types of parameterization like collection classes where there is little confusion on T or maybe K,V are. But I don't see that approach working well in this situation.
What are suggestions and how can I think about this differently? Ideally I'd like an approach that I can just reach for whenever this occurs and not have to come up with new names. The AbstractionTrait
for naming a trait or maybe TAbstraction
for naming a type parameter does not appear to be done in rust and I'm not sure why (too verbose?). I haven't coded C# but it looks like they address this similar naming issue as follows:
Names of Classes, Structs, and Interfaces
DO prefix descriptive type parameter names with
T
.
public interface ISessionChannel<TSession> where TSession : ISession {
TSession Session { get; }
}