Skip to content

Commit 4c1c3dd

Browse files
committed
✨Add Implicit From Attribute.
1 parent b443e9d commit 4c1c3dd

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

Tests/TedToolkit.Console/Absorptance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace TedToolkit.Quantities;
22

3+
[QuantityImplicitFromValueType]
34
[QuantityImplicitToValueType]
45
[QuantityOperator<AbsorbedDose, AbsorbedDose, double>(Operator.Divide)]
56
partial struct AbsorbedDose
@@ -10,5 +11,6 @@ public static void Test()
1011
var b = new AbsorbedDose(10, AbsorbedDoseUnit.Centigray);
1112
var c = a / b;
1213
double d = a;
14+
AbsorbedDose e = 10.0;
1315
}
1416
}

src/analyzers/TedToolkit.Quantities.Analyzer/QuantityStructGenerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public void GenerateCode(SourceProductionContext context)
442442
#region Conversions
443443

444444
ConversionOperatorDeclaration(
445-
Token(quantitySymbol?.GetAttributes().Any(a => a.AttributeClass?.GetName().FullName
445+
Token(quantitySymbol?.GetAttributes().Any(a => a.AttributeClass?.GetName().FullName
446446
is "global::TedToolkit.Quantities.QuantityImplicitToValueTypeAttribute") ?? false
447447
? SyntaxKind.ImplicitKeyword
448448
: SyntaxKind.ExplicitKeyword),
@@ -461,7 +461,11 @@ public void GenerateCode(SourceProductionContext context)
461461
IdentifierName("Value"))))
462462
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
463463

464-
ConversionOperatorDeclaration(Token(SyntaxKind.ExplicitKeyword),
464+
ConversionOperatorDeclaration(
465+
Token(quantitySymbol?.GetAttributes().Any(a => a.AttributeClass?.GetName().FullName
466+
is "global::TedToolkit.Quantities.QuantityImplicitFromValueTypeAttribute") ?? false
467+
? SyntaxKind.ImplicitKeyword
468+
: SyntaxKind.ExplicitKeyword),
465469
IdentifierName(quantity.Name))
466470
.WithModifiers(
467471
TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TedToolkit.Quantities;
2+
3+
/// <summary>
4+
/// Implicit convert from the value type
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Struct)]
7+
public sealed class QuantityImplicitFromValueTypeAttribute: Attribute;
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
namespace TedToolkit.Quantities;
22

3-
namespace TedToolkit.Quantities;
3+
/// <summary>
4+
/// Implicit convert to the value type
5+
/// </summary>
46
[AttributeUsage(AttributeTargets.Struct)]
5-
[SuppressMessage("ReSharper", "UnusedTypeParameter")]
6-
public sealed class QuantityImplicitToValueTypeAttribute: Attribute;
7+
public sealed class QuantityImplicitToValueTypeAttribute : Attribute;

0 commit comments

Comments
 (0)