On Friday, 8 November 2024 at 21:55:53 UTC, Liam McGillivray wrote:
enum Milligrams : BaseUnit!Milligrams;
enum Millimoles : BaseUnit!Millimoles;

These are forward declarations of enumeration types (aka opaque enums). These types do not inherit from the BaseUnit struct. Declaring the base type says that the member values of the enumeration must implicitly convert to the base type.

https://dlang.org/spec/enum.html#named_enums

        Milligrams mass = Milligrams(20f);

I'm not sure why that compiles.

        mass = 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`
```

Because an enumeration type does not inherit methods of its base type, there is no opAssign method for enum types.

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?

Then mass of type BaseUnit does have an opAssign member.

Reply via email to