ConstTypeArgs.Core
1.0.0
See the version list below for details.
dotnet add package ConstTypeArgs.Core --version 1.0.0
NuGet\Install-Package ConstTypeArgs.Core -Version 1.0.0
<PackageReference Include="ConstTypeArgs.Core" Version="1.0.0" />
paket add ConstTypeArgs.Core --version 1.0.0
#r "nuget: ConstTypeArgs.Core, 1.0.0"
// Install ConstTypeArgs.Core as a Cake Addin #addin nuget:?package=ConstTypeArgs.Core&version=1.0.0 // Install ConstTypeArgs.Core as a Cake Tool #tool nuget:?package=ConstTypeArgs.Core&version=1.0.0
ConstTypeArgs
The Const Type Args library provides a way to use type parameters to pass static & constant values to generic types & methods. These values, or const type arguments, are accessible at compile-time and can be used to define the behavior of a generic type or method, without the need to pass them as runtime arguments. This mimics template specialization in C++ and allows for a form of compile-time computation and value propagation. This is particularly useful in situations where compile-time safety and efficiency are important.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (20)
Showing the top 5 NuGet packages that depend on ConstTypeArgs.Core:
Package | Downloads |
---|---|
ConstTypeArgs.Ints
Builds on type of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass int values to generics at compile-time. This provides an analog to type specialization in C++, and can be used for scenarios such as: * Static configuration, * Eliminating unnecessary instance constructors, * "Passing" values to type initializers, * And more. Built-in const type arguments cover -1 to -15, 0 to 15, powers of 2 up to 65536, and more. Here's a simple demonstration showing how to define and use const type arguments and domain-specific type arguments: using ConstTypeArgs.Ints; // Const type arguments: public readonly struct _8 : K_Int<_8> { public static int Value => 8; } public readonly struct _32 : K_Int<_32> { public static int Value => 32; } public abstract class DefaultSize : Int<_32> { } // Usage: public class Foo<TSize> where TSize : K_Int { public static readonly int[] FooArray = new int[TSize.Value]; static Foo() { Console.WriteLine($"Array size is {FooArray.Length}"); } } // Elsewhere var foo = new Foo<_8>(); // Outputs "Array size is 8" foo = new Foo<DefaultSize>(); // Outputs "Array size is 32" |
|
ConstTypeArgs.UInt128s
Builds on type of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass UInt128 values to generics at compile-time. This provides an analog to type specialization in C++, and can be used for scenarios such as: * Static configuration, * Eliminating unnecessary instance constructors, * "Passing" values to type initializers, * And more. Built-in const type arguments include values for 0 to 16 and others. Here's a simple demonstration showing how to define and use const type arguments and domain-specific type arguments: using ConstTypeArgs.UInt128s; // Const type arguments: public readonly struct _8 : K_UInt128<_8> { public static UInt128 Value => 8; } public readonly struct _64_000 : K_UInt128<_64_000> { public static UInt128 Value => 64000; } public abstract class DefaultSize : UInt128<_64_000> { } // Usage: public class Foo<TSize> where TSize : K_UInt128 { public static readonly UInt128 BigArraySize = TSize.Value; // Code to initialize very large arrays. static Foo() { Console.WriteLine($"Big array size is {BigArraySize.Length}"); } } // Elsewhere var foo = new Foo<_8>(); // Outputs "Big array size is 8" foo = new Foo<DefaultSize>(); // Outputs "Big array size is 32" |
|
ConstTypeArgs.Bytes
Builds on type of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass byte values to generics at compile-time. This provides an analog to type specialization in C++, and can be used for scenarios such as: * Static configuration, * Eliminating unnecessary instance constructors, * "Passing" values to type initializers, * And more. Built-in const type arguments include values for 0 - 10, powers of 2, min & max values, and others. Here's a simple demonstration showing how to define and use const type arguments and domain-specific type arguments: using ConstTypeArgs.Bytes; // Const type arguments: public readonly struct _8 : K_Byte<_8> { public static byte Value => 8; } public readonly struct _32 : K_Byte<_32> { public static byte Value => 32; } public abstract class InitialValue : Byte<_32> { } // Usage: public class Foo<TSize> where TSize : K_Byte { public static readonly int[] FooArray = new int[TSize.Value]; static Foo() { Console.WriteLine($"Array size is {FooArray.Length}"); } } // Elsewhere var foo = new Foo<_8>(); // Outputs "Array size is 8" foo = new Foo<InitialValue>(); // Outputs "Array size is 32" |
|
ConstTypeArgs.Ulongs
Builds on type of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass ulong values to generics at compile-time. This provides an analog to type specialization in C++, and can be used for scenarios such as: * Static configuration, * Eliminating unnecessary instance constructors, * "Passing" values to type initializers, * And more. Built-in const type arguments cover 0 to 15, powers of 2 up to 65536, and more. Here's a simple demonstration showing how to define and use const type arguments and domain-specific type arguments: using ConstTypeArgs.Ulongs; // Const type arguments: public readonly struct _8 : K_Ulong<_8> { public static ulong Value => 8; } public readonly struct _32 : K_Ulong<_32> { public static ulong Value => 32; } public abstract class DefaultSize : Ulong<_32> { } // Usage: public class Foo<TSize> where TSize : K_Ulong { public static readonly int[] FooArray = new int[TSize.Value]; static Foo() { Console.WriteLine($"Array size is {FooArray.Length}"); } } // Elsewhere var foo = new Foo<_8>(); // Outputs "Array size is 8" foo = new Foo<DefaultSize>(); // Outputs "Array size is 32" |
|
ConstTypeArgs.Types
Builds on types of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass Type values to generics at compile-time. This provides an analog to type specialization in C++. Here's a simple demonstration: using ConstTypeArgs.Types; public class Foo<TType> { static Foo() { if (TType.Value == typeof(string) || TType.Value == typeof(char) || TType.Value == typeof(char[]) || TType.Value == typeof(ReadOnlyMemory<char>) || TType.Value == typeof(ReadOnlySpan<char>)) { Console.WriteLine($"{TType.Value} values usually represent text!"); } if (TType.Value == typeof(byte) || TType.Value == typeof(sbyte) || TType.Value == typeof(short) || TType.Value == typeof(ushort) || TType.Value == typeof(int) || TType.Value == typeof(uint) || TType.Value == typeof(long) || TType.Value == typeof(ulong) || TType.Value == typeof(nint) || TType.Value == typeof(nuint)) { Console.WriteLine($"{TType.Value} values usually represent numbers!"); } } } // Elsewhere, assuming the value of StringType is typeof(string) var foo = new Foo<StringType>(); // Outputs: StringType values usually represent text! // Assuming that the value of IntType is typeof(int) foo = new Foo<IntType>(); // Outputs: TType values usually represent numbers! Here's how you could create a new Type const type argument: public readonly struct StringType : K_Type<StringType> { public static Type Value => typeof(string); } |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.0 | 113 | 8/27/2024 |
1.2.0-beta1 | 111 | 8/20/2024 |
1.0.0 | 291 | 7/1/2024 |
Initial release.