-
Notifications
You must be signed in to change notification settings - Fork 386
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
XP: Prototype source generator #1500
base: release/v6
Are you sure you want to change the base?
Conversation
This is a great idea! 👍 How about combining source generation with @lipchev's proposal of replacing the underlying Value type, to something the user can decide, Something similar could be done by making the quantities generic over the underlying Value type and than aliasing them but I guess source generators can solve multiple problems at once. While we're at it, what are your thoughts on giving quantities a static base unit to avoid loss of precision and comparison issues? Users can then choose a base unit that makes sense for their project, or even multiple and avoid mixing them up:
With source generation we can make some things very explicit:
|
namespace UnitsNet.Xp.SrcGen | ||
{ | ||
[Generator] | ||
public class QuantitySourceGenerator : ISourceGenerator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ISourceGenerator
has been deprecated in favor of IIncrementalGenerator
I was also thinking of (eventually) offering a source generator to facilitate the creation of "custom" quantities, but I don't believe its possible to have the source-generated quantities as a replacement of the current nugget: consider how multiple projects, each with it's own source generator would interact- is the FYI- my fraction-based implementation requires only the I also considered using a |
Investigate if we can split up UnitsNet by using c# source generators to only bring in quantities and operator overloads for the quantities you intend to use.
Benefits
Speed
,Length
andDuration
were provided in separate nugets to provide arithemetic operator overloads likeSpeed = Length / Duration
, which quantity nugets should provide what overloads and which nugets should they depend on?Proposal (not fleshed out yet)
UnitsNet.Core
nugetIQuantity
,QuantityInfo
,UnitsNetSetup
etc.[assembly: UnitsNetSrcGenInit("Length,Mass")]
to bring inLength
andMass
quantities, and any arithmetic operators for theseUnitsNet.DefaultUnits
nugetUnitsNet
nuget, brings in both of the above to mimic today's features out of the box - except built on source generators and only building what you actually useExample
Declaring the quantities to generate:
https://github.com/angularsen/UnitsNet/pull/1500/files#diff-f671be4e1e6fa64f602565c6d10f3c035f90d5c2ea631709d7e8ecb0d48537c9
Using the generated quantities:
https://github.com/angularsen/UnitsNet/pull/1500/files#diff-3c14d449aa45f6d30c596b65d3d6f0625b03212c7cf3028d3c905b7db57e37c4