I am working on a library for making types representing units of measurement. Base units are derived from a struct called `BaseUnit`. I want to make it so that `BaseUnit`-derived types can have their values assigned from float constants, but not from each-other or from float variables.

I just want to know why the compiler doesn't recognize the `opAssign` function I have.
```
struct BaseUnit(TThis) {
    float value;

    this(T)(T v) if(isNumeric!T) {
        value = v;
    }

    void opAssign(T)(const T v) if(isNumeric!T) {
        value = v;
    }
}

enum Milligrams : BaseUnit!Milligrams;
enum Millimoles : BaseUnit!Millimoles;

unittest {
        Milligrams mass = Milligrams(20f);
        assert(mass==Milligrams(20f));
        mass = 40f;
        assert(mass.value==40f);
}
```

That line in the unit test assigning `40f` to `mass` is giving me an error:
```
Error: cannot implicitly convert expression `40.0F` of type `float` to `Milligrams`
```

However, if I were to change `BaseUnit` into a struct (not a struct template), and make `mass` of `BaseUnit` rather than `Milligrams`, then the `opAssign` function works as intended.

What's going on?

Reply via email to