Skip to content

Add Dimension type to Unit trait #367

@ttencate

Description

@ttencate

Related to #231, but I think it's complementary.

Right now, the generated Unit trait makes it difficult to write methods that work generically over different types of Units. For example:

fn do_work<N>(value: f64) -> Quantity<D, U, f64>
    where N: Unit
{
     Quantity::new::<N>(value) // Error! Cannot infer type of D and U
}

It would be great if some associated types could be added to the Unit trait:

pub trait Unit: Copy {
    /// The dimension of this unit.
    type Dimension: Dimension;
    /// The base units of this unit.
    type Units: Units;

    ...
}

So that we can write:

fn do_work<N>(value: f64) -> Quantity<D, U, f64> 
    where N: Unit
{
     Quantity::<N::Dimension, N::Units, _>::new::<N>(value)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions