StronglyTypedPrimitives 0.0.0-alpha.0.23
This is a prerelease version of StronglyTypedPrimitives.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package StronglyTypedPrimitives --version 0.0.0-alpha.0.23
NuGet\Install-Package StronglyTypedPrimitives -Version 0.0.0-alpha.0.23
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StronglyTypedPrimitives" Version="0.0.0-alpha.0.23" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StronglyTypedPrimitives --version 0.0.0-alpha.0.23
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: StronglyTypedPrimitives, 0.0.0-alpha.0.23"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StronglyTypedPrimitives as a Cake Addin #addin nuget:?package=StronglyTypedPrimitives&version=0.0.0-alpha.0.23&prerelease // Install StronglyTypedPrimitives as a Cake Tool #tool nuget:?package=StronglyTypedPrimitives&version=0.0.0-alpha.0.23&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Strongly Typed Primitives
A source generator for creating strongly-typed primitive types that makes it easy to avoid the primitive obsession anti pattern.
Add a [StronglyTyped]
attribute to a partial record struct to get started, for example:
using StronglyTypedPrimitives;
[StronglyTyped]
public partial record struct Foo(int Value);
And the following code is generated for you:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedPrimitives, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "1.0.0.0")]
public readonly partial record struct Foo : global::StronglyTypedPrimitives.IStronglyTypedPrimitive<int>, global::System.IParsable<global::SomeNamespace.Foo>, global::System.ISpanParsable<global::SomeNamespace.Foo>, global::System.IUtf8SpanParsable<global::SomeNamespace.Foo>, global::System.IComparable<global::SomeNamespace.Foo>, global::System.IComparable, global::System.IFormattable, global::System.ISpanFormattable, global::System.IUtf8SpanFormattable
{
public static readonly Foo Empty = new Foo(default);
private readonly int @value = ThrowIfValueIsInvalid(Value);
public int Value
{
get => @value;
init
{
@value = ThrowIfValueIsInvalid(value);
}
}
private static int ThrowIfValueIsInvalid(int value)
{
IsValueValid(value, throwIfInvalid: true);
return value;
}
public override string ToString() => Value.ToString();
public static bool IsValueValid(int value, bool throwIfInvalid)
=> true;
public static Foo Parse(string s, System.IFormatProvider? provider)
{
var rawValue = int.Parse(s, provider);
IsValueValid(rawValue, throwIfInvalid: true);
return new Foo(rawValue);
}
public static bool TryParse(string? s, System.IFormatProvider? provider, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(returnValue: false)] out SomeNamespace.Foo result)
{
if (int.TryParse(s, provider, out var rawValue) && IsValueValid(rawValue, throwIfInvalid: false))
{
result = new Foo(rawValue);
return true;
}
result = Foo.Empty;
return false;
}
public static Foo Parse(System.ReadOnlySpan<char> s, System.IFormatProvider? provider)
{
var rawValue = int.Parse(s, provider);
IsValueValid(rawValue, throwIfInvalid: true);
return new Foo(rawValue);
}
public static bool TryParse(System.ReadOnlySpan<char> s, System.IFormatProvider? provider, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(returnValue: false)] out SomeNamespace.Foo result)
{
if (int.TryParse(s, provider, out var rawValue) && IsValueValid(rawValue, throwIfInvalid: false))
{
result = new Foo(rawValue);
return true;
}
result = Foo.Empty;
return false;
}
public static Foo Parse(System.ReadOnlySpan<byte> utf8Text, System.IFormatProvider? provider)
{
var rawValue = int.Parse(utf8Text, provider);
IsValueValid(rawValue, throwIfInvalid: true);
return new Foo(rawValue);
}
public static bool TryParse(System.ReadOnlySpan<byte> utf8Text, System.IFormatProvider? provider, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(returnValue: false)] out SomeNamespace.Foo result)
{
if (int.TryParse(utf8Text, provider, out var rawValue) && IsValueValid(rawValue, throwIfInvalid: false))
{
result = new Foo(rawValue);
return true;
}
result = Foo.Empty;
return false;
}
public int CompareTo(SomeNamespace.Foo other)
=> Value.CompareTo(other.Value);
public int CompareTo(object? obj)
{
if (obj is null)
{
return 1;
}
if (obj is Foo other)
{
return Value.CompareTo(other.Value);
}
return ((System.IComparable)Value).CompareTo(obj);
}
public string ToString(string? format, System.IFormatProvider? formatProvider)
=> Value.ToString(format, formatProvider);
public bool TryFormat(System.Span<char> destination, out int charsWritten, System.ReadOnlySpan<char> format, System.IFormatProvider? provider)
=> ((System.ISpanFormattable)Value).TryFormat(destination, out charsWritten, format, provider);
public bool TryFormat(System.Span<byte> utf8Destination, out int bytesWritten, System.ReadOnlySpan<char> format, System.IFormatProvider? provider)
=> ((System.IUtf8SpanFormattable)Value).TryFormat(utf8Destination, out bytesWritten, format, provider);
public static bool operator > (Foo a, Foo b) => a.CompareTo(b) > 0;
public static bool operator < (Foo a, Foo b) => a.CompareTo(b) < 0;
public static bool operator >=(Foo a, Foo b) => a.CompareTo(b) >= 0;
public static bool operator <=(Foo a, Foo b) => a.CompareTo(b) <= 0;
}
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.0-alpha.0.26 | 49 | 2/26/2025 |
0.0.0-alpha.0.24 | 52 | 2/26/2025 |
0.0.0-alpha.0.23 | 53 | 2/26/2025 |
0.0.0-alpha.0.10 | 49 | 2/26/2025 |