Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to define arithmetic operators separately #30

Merged
merged 12 commits into from
Aug 17, 2023

Conversation

hadashiA
Copy link
Member

@hadashiA hadashiA commented Aug 17, 2023

EDIT: INumber<T> implementation was stopped.

If UnitGenerateOptions.ArithmeticOperator is specified, make sure that all members conforming to INumber<T> are created by default.
This allows UnitOf generated types to take advantage of features supported by Generic math. NET8 or later, for example, LINQ's aggregate would be available.

Add extra option UnitArithmeticOperators

  • Added UnitArithmeticOperators enum to allow specifying the individual arithmetic operators to be generated.
  • When specifying individual arithmetic operators, an interface declaration for the arithmetic operator is added, such as IAdditionalOperators<>, which was added in .

Breaking Changes

  • operator ++, operator-- generation conditions
    • Before:
      • When UnitGenerateOptions.ValueArithmeticOperator
    • After:
      • When UnitGenerateOptions.ArithmeticOperator, with UnitArithmeticOperators.Number (default) or .Increment / .Decrement
  • UnitOf arguments
    • Add named params as [UnitOf(..., ArithmeticOperator = ...]
    • In addition,toStringFormat is changed to a named parameter from 3rd argument.

public static Hp operator -(in Hp x, in Hp y) => new Hp(checked((int)(x.value - y.value)));
public static Hp operator *(in Hp x, in Hp y) => new Hp(checked((int)(x.value * y.value)));
public static Hp operator /(in Hp x, in Hp y) => new Hp(checked((int)(x.value / y.value)));
public static Hp operator +(Hp x, Hp y) => new Hp(checked((int)(x.value + y.value)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The in modifiers was removed because it could not comply with interfaces such as INumber<>.


public static bool TryConvertFromChecked<TOther>(TOther value, out {{unitTypeName}} result) where TOther : INumberBase<TOther>
{
throw new NotSupportedException();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods ofTryConvert* family are in INumberBase<T> but cannot be called because they are protected. I compromised and made it NotSupported....
Please let me know if you have a better way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed implementation to return false.

@hadashiA hadashiA changed the title Support System.Numerics.INumber<T>, and more options to specify operators individually. Add options to define arithmetic operators separately Aug 17, 2023
@neuecc
Copy link
Member

neuecc commented Aug 17, 2023

thank you! ok to merge!

@hadashiA hadashiA merged commit 41a364b into master Aug 17, 2023
2 checks passed
@hadashiA hadashiA deleted the hadashiA/numeric branch August 17, 2023 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants