├── FSharpBenchmark ├── README.md ├── FSharpBenchmark.fsproj └── Program.fs ├── InductiveVariadics.Attributes ├── InductiveVariadics.Attributes.csproj └── Attributes.cs ├── InductiveVariadics.Sample ├── CountSizes.cs ├── InductiveVariadics.Sample.csproj ├── Program.cs ├── Concat.cs └── Add.cs ├── CSharpBenchmark ├── CSharpBenchmark.csproj └── Program.cs ├── InductiveVariadics.SourceGenerator ├── InductiveVariadics.SourceGenerator.csproj └── SourceGenerator.cs ├── .vscode ├── tasks.json └── launch.json ├── LICENSE ├── InductiveVariadics.sln ├── README.md └── .gitignore /FSharpBenchmark/README.md: -------------------------------------------------------------------------------- 1 | # F# Benchmark 2 | 3 | This thing uses computation expressions. Suggested by [@gsomix](https://github.com/gsomix). 4 | 5 | -------------------------------------------------------------------------------- /InductiveVariadics.Attributes/InductiveVariadics.Attributes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | enable 6 | preview 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /FSharpBenchmark/FSharpBenchmark.fsproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /InductiveVariadics.Sample/CountSizes.cs: -------------------------------------------------------------------------------- 1 | 2 | using InductiveVariadics; 3 | using System.Runtime.InteropServices; 4 | 5 | partial class Sizes 6 | { 7 | [InductionBaseOf("CountSizes")] 8 | public static int CountBase() => 0; 9 | 10 | 11 | [InductionTransitionOf("CountSizes")] 12 | public static int CountTransition(T value, int folded) 13 | { 14 | System.Console.WriteLine($"Size of {value} is {Marshal.SizeOf()}"); 15 | return Marshal.SizeOf() + folded; 16 | } 17 | 18 | [InductionFinalizationOf("CountSizes")] 19 | public static int CountFinalization(int folded) => folded; 20 | } 21 | -------------------------------------------------------------------------------- /InductiveVariadics.Attributes/Attributes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InductiveVariadics; 4 | 5 | public sealed class InductionBaseOfAttribute : Attribute 6 | { 7 | public string Name { get; } 8 | public InductionBaseOfAttribute(string name) => Name = name; 9 | } 10 | 11 | public sealed class InductionTransitionOfAttribute : Attribute 12 | { 13 | public string Name { get; } 14 | public InductionTransitionOfAttribute(string name) => Name = name; 15 | } 16 | 17 | public sealed class InductionFinalizationOfAttribute : Attribute 18 | { 19 | public string Name { get; } 20 | public InductionFinalizationOfAttribute(string name) => Name = name; 21 | } 22 | -------------------------------------------------------------------------------- /InductiveVariadics.Sample/InductiveVariadics.Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /CSharpBenchmark/CSharpBenchmark.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /InductiveVariadics.SourceGenerator/InductiveVariadics.SourceGenerator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | enable 6 | preview 7 | 8 | 9 | 10 | 11 | $(NoWarn);RS2000;RS2001;RS2002;RS2003;RS2004;RS2005;RS2006;RS2007;RS2008 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /InductiveVariadics.Sample/Program.cs: -------------------------------------------------------------------------------- 1 | // See https://aka.ms/new-console-template for more information 2 | 3 | using System; 4 | using System.Linq; 5 | using System.Runtime.CompilerServices; 6 | using System.Text; 7 | using BenchmarkDotNet.Attributes; 8 | using BenchmarkDotNet.Running; 9 | using InductiveVariadics; 10 | 11 | 12 | Console.WriteLine( 13 | Stuff.Concat(1, 2.56f, "aaa", 4343.3) 14 | ); 15 | 16 | 17 | 18 | Console.WriteLine(Arithmetics.Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); 19 | Console.WriteLine(); 20 | Console.WriteLine(Sizes.CountSizes(0, 1.2, 3.3m)); 21 | 22 | Sum(1, 2, 3, 4); 23 | // equivalent to 24 | Sum(new int[] { 1, 2, 3, 4 }); 25 | 26 | static int Sum(params int[] objects) 27 | => objects.Sum(); 28 | 29 | // BenchmarkRunner.Run(); 30 | // Console.WriteLine(new Bench().AddParams()); 31 | // Console.WriteLine(new Bench().AddVariadics()); 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build debug", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/InductiveVariadics.Sample/InductiveVariadics.Sample.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "build solution release", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "build", 22 | "-c", 23 | "release", 24 | "${workspaceFolder}", 25 | "/property:GenerateFullPaths=true", 26 | "/consoleloggerparameters:NoSummary" 27 | ], 28 | "problemMatcher": "$msCompile" 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /InductiveVariadics.Sample/Concat.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using InductiveVariadics; 3 | 4 | partial class Stuff 5 | { 6 | [InductionBaseOf("Concat")] 7 | public static StringBuilder ConcatBase() => new(200); 8 | 9 | 10 | 11 | [InductionTransitionOf("Concat")] 12 | public static StringBuilder ConcatTransition(string value, StringBuilder folded) 13 | => folded.Append(value); 14 | 15 | [InductionTransitionOf("Concat")] 16 | public static StringBuilder ConcatTransition(int value, StringBuilder folded) 17 | => folded.Append(value); 18 | 19 | [InductionTransitionOf("Concat")] 20 | public static StringBuilder ConcatTransition(float value, StringBuilder folded) 21 | => folded.Append(value); 22 | 23 | [InductionTransitionOf("Concat")] 24 | public static StringBuilder ConcatTransition(double value, StringBuilder folded) 25 | => folded.Append(value); 26 | 27 | 28 | 29 | [InductionFinalizationOf("Concat")] 30 | public static string ConcatFinalize(StringBuilder sb) => sb.ToString(); 31 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 WhiteBlackGoose 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /FSharpBenchmark/Program.fs: -------------------------------------------------------------------------------- 1 | open System 2 | open BenchmarkDotNet.Attributes 3 | open BenchmarkDotNet.Running 4 | 5 | type FoldStep<'State, 'T> = ('State -> 'T -> 'State) -> 'State -> 'State 6 | 7 | type FoldBuilder() = 8 | member this.Zero(): FoldStep<'State, 'T> = 9 | fun _ state -> state 10 | 11 | member inline this.Combine( 12 | [] f: FoldStep<'State, 'T>, 13 | [] g: FoldStep<'State, 'T>) 14 | : FoldStep<'State, 'T> = 15 | fun folder state -> g folder (f folder state) 16 | 17 | member inline this.Delay([] f: unit -> FoldStep<'State, 'T>) = 18 | f() 19 | 20 | member inline this.Yield(value: 'T): FoldStep<'State, 'T> = 21 | fun folder state -> folder state value 22 | 23 | type Bench() = 24 | let fold = FoldBuilder() 25 | 26 | [] 27 | member _.addVariadics () = 28 | (fold { 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 }) (+) 0 29 | 30 | 31 | 32 | // (Bench ()).addVariadics() |> printfn "%i" 33 | 34 | BenchmarkRunner.Run() |> ignore 35 | 36 | -------------------------------------------------------------------------------- /InductiveVariadics.Sample/Add.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using InductiveVariadics; 3 | using System.Runtime.CompilerServices; 4 | 5 | partial class Arithmetics 6 | { 7 | [InductionBaseOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 8 | public static int AddBase() => 0; 9 | 10 | 11 | 12 | [InductionTransitionOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 13 | public static int AddTransition(int head, int folded) => head + folded; 14 | 15 | 16 | 17 | [InductionFinalizationOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 18 | public static int AddFinalization(int folded) => folded; 19 | 20 | public static int AddNaive(params int[] ints) 21 | { 22 | var a = 0; 23 | foreach (var i in ints) 24 | a += i; 25 | return a; 26 | } 27 | } 28 | 29 | [MemoryDiagnoser] 30 | public class Bench 31 | { 32 | [Benchmark] 33 | public int AddVariadics() 34 | => Arithmetics.Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 35 | 36 | [Benchmark] 37 | public int AddParams() 38 | => Arithmetics.AddNaive(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 39 | } -------------------------------------------------------------------------------- /CSharpBenchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using InductiveVariadics; 3 | using System.Runtime.CompilerServices; 4 | using BenchmarkDotNet.Running; 5 | using System; 6 | 7 | //Console.WriteLine(new Bench().AddVariadics()); 8 | 9 | BenchmarkRunner.Run(); 10 | 11 | partial class Arithmetics 12 | { 13 | [InductionBaseOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 14 | public static int AddBase() => 0; 15 | 16 | 17 | 18 | [InductionTransitionOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 19 | public static int AddTransition(int head, int folded) => head + folded; 20 | 21 | 22 | 23 | [InductionFinalizationOf("Add"), MethodImpl(MethodImplOptions.AggressiveInlining)] 24 | public static int AddFinalization(int folded) => folded; 25 | 26 | public static int AddNaive(params int[] ints) 27 | { 28 | var a = 0; 29 | foreach (var i in ints) 30 | a += i; 31 | return a; 32 | } 33 | } 34 | 35 | [MemoryDiagnoser] 36 | public class Bench 37 | { 38 | [Benchmark] 39 | public int AddVariadics() 40 | => Arithmetics.Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 41 | 42 | [Benchmark] 43 | public int AddParams() 44 | => Arithmetics.AddNaive(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 45 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Run Debug", 6 | "type": "coreclr", 7 | "request": "launch", 8 | "preLaunchTask": "build debug", 9 | "program": "${workspaceFolder}/InductiveVariadics.Sample/bin/Debug/net6.0/InductiveVariadics.Sample.dll", 10 | "args": [], 11 | "cwd": "${workspaceFolder}/InductiveVariadics.Sample", 12 | "console": "internalConsole", 13 | "stopAtEntry": false 14 | }, 15 | { 16 | "name": "C# Benchmark", 17 | "type": "coreclr", 18 | "request": "launch", 19 | "preLaunchTask": "build solution release", 20 | "program": "${workspaceFolder}/CSharpBenchmark/bin/release/net6.0/CSharpBenchmark.dll", 21 | "args": [], 22 | "cwd": "${workspaceFolder}/CSharpBenchmark", 23 | "console": "internalConsole", 24 | "stopAtEntry": false 25 | }, 26 | { 27 | "name": "F# Benchmark", 28 | "type": "coreclr", 29 | "request": "launch", 30 | "preLaunchTask": "build solution release", 31 | "program": "${workspaceFolder}/FSharpBenchmark/bin/release/net6.0/FSharpBenchmark.dll", 32 | "args": [], 33 | "cwd": "${workspaceFolder}/FSharpBenchmark", 34 | "console": "internalConsole", 35 | "stopAtEntry": false 36 | } 37 | ] 38 | } -------------------------------------------------------------------------------- /InductiveVariadics.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.31911.260 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InductiveVariadics.Sample", "InductiveVariadics.Sample\InductiveVariadics.Sample.csproj", "{00AF75F9-9C8C-45C6-9A59-A9F387859AE8}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InductiveVariadics.SourceGenerator", "InductiveVariadics.SourceGenerator\InductiveVariadics.SourceGenerator.csproj", "{7C96F995-8F2A-4109-8CF4-90ADDC558C77}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InductiveVariadics.Attributes", "InductiveVariadics.Attributes\InductiveVariadics.Attributes.csproj", "{CC063976-1566-4FF8-AE71-90A083EE4E8B}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpBenchmark", "CSharpBenchmark\CSharpBenchmark.csproj", "{5B31455C-D6C2-4B22-9924-045875B14AB6}" 13 | EndProject 14 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpBenchmark", "FSharpBenchmark\FSharpBenchmark.fsproj", "{F9EF0946-0AD2-4CEB-B135-4D7F2FB0936A}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {00AF75F9-9C8C-45C6-9A59-A9F387859AE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {00AF75F9-9C8C-45C6-9A59-A9F387859AE8}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {00AF75F9-9C8C-45C6-9A59-A9F387859AE8}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {00AF75F9-9C8C-45C6-9A59-A9F387859AE8}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {7C96F995-8F2A-4109-8CF4-90ADDC558C77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {7C96F995-8F2A-4109-8CF4-90ADDC558C77}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {7C96F995-8F2A-4109-8CF4-90ADDC558C77}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {7C96F995-8F2A-4109-8CF4-90ADDC558C77}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {CC063976-1566-4FF8-AE71-90A083EE4E8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {CC063976-1566-4FF8-AE71-90A083EE4E8B}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {CC063976-1566-4FF8-AE71-90A083EE4E8B}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {CC063976-1566-4FF8-AE71-90A083EE4E8B}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {5B31455C-D6C2-4B22-9924-045875B14AB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {5B31455C-D6C2-4B22-9924-045875B14AB6}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {5B31455C-D6C2-4B22-9924-045875B14AB6}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {5B31455C-D6C2-4B22-9924-045875B14AB6}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {F9EF0946-0AD2-4CEB-B135-4D7F2FB0936A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {F9EF0946-0AD2-4CEB-B135-4D7F2FB0936A}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {F9EF0946-0AD2-4CEB-B135-4D7F2FB0936A}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {F9EF0946-0AD2-4CEB-B135-4D7F2FB0936A}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(ExtensibilityGlobals) = postSolution 47 | SolutionGuid = {1E018473-FD21-459E-ABA0-736430092966} 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > **Warning**: this is no more than a proof of concept repo 2 | 3 | # Inductive Variadics 4 | 5 | Variadic arguments for C# with inductive declaration. Made via C# 9's source generators. 6 | 7 | Many heard of variadic arguments in C++. In my IVG PoC (proof of concept of inductive variadic generics) the idea 8 | is that for a sequence of parameters and types it is in fact a base, a transition from `n` to `n + 1`, and finalization (post-processing). 9 | 10 | So, what I mean is, assume you have this: 11 | 12 | ```cs 13 | var concatenated = Concat(123, "aaaa", 3.5); 14 | ``` 15 | 16 | So by default there's a default string builder (even for 0 args): 17 | 18 | ```cs 19 | [InductionBaseOf("Concat")] 20 | public static StringBuilder ConcatBase() 21 | => new(); 22 | ``` 23 | 24 | At this point we just create an instance. 25 | 26 | Now, given that we already have some `f(n)`, like in math, we define `f(n + 1)`: 27 | 28 | ```cs 29 | [InductionTransitionOf("Concat")] 30 | public static StringBuilder ConcatTransition(T value, StringBuilder folded) 31 | { 32 | if (typeof(T) == typeof(int)) 33 | folded.Append((int)((object)value!)!); 34 | else if (typeof(T) == typeof(float)) 35 | folded.Append((float)((object)value!)!); 36 | else 37 | folded.Append(value); 38 | return folded; 39 | } 40 | ``` 41 | 42 | So we get a new `value` of some type, and already processed `StringBuilder`. 43 | 44 | Finally, to get a string out of the string builder, we finalize: 45 | 46 | ```cs 47 | [InductionFinalizationOf("Concat")] 48 | public static string ConcatFinalize(StringBuilder sb) 49 | => sb.ToString(); 50 | ``` 51 | 52 | Now, when you write 53 | ```cs 54 | var concatenated = Concat(123, "aaaa", 3.5); 55 | ``` 56 | 57 | The following code is generated: 58 | ```cs 59 | public static string Concat(T1 value1, T2 value2, T3 value3) 60 | { 61 | var value = ConcatBase(); 62 | value = ConcatTransition(value1, value); 63 | value = ConcatTransition(value2, value); 64 | value = ConcatTransition(value3, value); 65 | return ConcatFinalize(value); 66 | } 67 | ``` 68 | 69 | Voila! 70 | 71 | 72 | ## Examples 73 | 74 | 75 | ### Add integers 76 | 77 | The simplest case to add n integers. 78 | 79 | ```cs 80 | Console.WriteLine(Arithmetics.Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); 81 | 82 | partial class Arithmetics 83 | { 84 | [InductionBaseOf("Add")] 85 | public static int AddBase() => 0; 86 | 87 | [InductionTransitionOf("Add")] 88 | public static int AddTransition(int head, int folded) => head + folded; 89 | 90 | [InductionFinalizationOf("Add")] 91 | public static int AddFinalization(int folded) => folded; 92 | } 93 | ``` 94 | 95 | If we compare that to 96 | ```cs 97 | public static int AddNaive(params int[] ints) 98 | { 99 | var a = 0; 100 | foreach (var i in ints) 101 | a += i; 102 | return a; 103 | } 104 | ``` 105 | we get: 106 | 107 | 108 | | Method | Mean | Error | StdDev | Gen 0 | Allocated | 109 | |------------- |----------:|----------:|----------:|-------:|----------:| 110 | | AddVariadics | 8.241 ns | 0.2033 ns | 0.4199 ns | - | - | 111 | | AddParams | 28.389 ns | 0.6595 ns | 1.9133 ns | 0.0459 | 144 B | 112 | 113 | ### Concat objects 114 | 115 | To avoid boxing we want to call StringBuilder's overloads for int, double, etc. 116 | 117 | So we make multiple overloads for transition. 118 | 119 | ```cs 120 | Console.WriteLine(Stuff.Concat(1, 2.56f, "aaa", 4343)); 121 | 122 | partial class Stuff 123 | { 124 | [InductionBaseOf("Concat")] 125 | public static StringBuilder ConcatBase() => new(200); 126 | 127 | 128 | 129 | [InductionTransitionOf("Concat")] 130 | public static StringBuilder ConcatTransition(string value, StringBuilder folded) 131 | => folded.Append(value); 132 | 133 | [InductionTransitionOf("Concat")] 134 | public static StringBuilder ConcatTransition(int value, StringBuilder folded) 135 | => folded.Append(value); 136 | 137 | [InductionTransitionOf("Concat")] 138 | public static StringBuilder ConcatTransition(float value, StringBuilder folded) 139 | => folded.Append(value); 140 | 141 | [InductionTransitionOf("Concat")] 142 | public static StringBuilder ConcatTransition(double value, StringBuilder folded) 143 | => folded.Append(value); 144 | 145 | 146 | 147 | [InductionFinalizationOf("Concat")] 148 | public static string ConcatFinalize(StringBuilder sb) => sb.ToString(); 149 | } 150 | ``` 151 | 152 | ### Sum sizes of objects and print them 153 | 154 | Finally, the most advanced case is to make transition generic. 155 | 156 | ```cs 157 | Console.WriteLine(Sizes.CountSizes(0, 1.2, 3.3m)); 158 | 159 | partial class Sizes 160 | { 161 | [InductionBaseOf("CountSizes")] 162 | public static int CountBase() => 0; 163 | 164 | 165 | [InductionTransitionOf("CountSizes")] 166 | public static int CountTransition(T value, int folded) 167 | { 168 | System.Console.WriteLine($"Size of {value} is {Marshal.SizeOf()}"); 169 | return Marshal.SizeOf() + folded; 170 | } 171 | 172 | [InductionFinalizationOf("CountSizes")] 173 | public static int CountFinalization(int folded) => folded; 174 | } 175 | 176 | } 177 | ``` 178 | 179 | ## Inspiration 180 | 181 | This proof of concept is inspired by [**this**](https://github.com/FiniteReality/VariadicGenerics) project by [FiniteReality](https://github.com/FiniteReality). 182 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /InductiveVariadics.SourceGenerator/SourceGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Microsoft.CodeAnalysis; 5 | using Microsoft.CodeAnalysis.CSharp.Syntax; 6 | using System.Diagnostics; 7 | 8 | namespace InductiveVariadics; 9 | 10 | public static class ree 11 | { 12 | public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) 13 | { 14 | key = pair.Key; 15 | value = pair.Value; 16 | } 17 | } 18 | 19 | [Generator] 20 | public sealed class VariadicGenerator : ISourceGenerator 21 | { 22 | internal record struct Sequence(IEnumerable Seq) where T : notnull 23 | { 24 | public bool Equals(Sequence other) 25 | { 26 | return Seq.SequenceEqual(other.Seq); 27 | } 28 | 29 | // TODO 30 | public override int GetHashCode() 31 | { 32 | var sum = 0; 33 | foreach (var el in Seq) 34 | sum += el.GetHashCode(); 35 | return sum; 36 | } 37 | 38 | } 39 | 40 | internal sealed class InductionBuilder 41 | { 42 | public (ITypeSymbol, IMethodSymbol)? Base { get; set; } 43 | public IMethodSymbol? Transition { get; set; } 44 | public (ITypeSymbol, IMethodSymbol)? Finalization { get; set; } 45 | 46 | public Induction ToInduction() 47 | => new(Base ?? throw new(), Transition ?? throw new(), Finalization ?? throw new()); 48 | } 49 | 50 | internal record struct Induction((ITypeSymbol, IMethodSymbol) Base, IMethodSymbol Transition, (ITypeSymbol, IMethodSymbol) Finalization); 51 | 52 | public void Execute(GeneratorExecutionContext context) 53 | { 54 | var receiver = (SyntaxReceiver)context.SyntaxContextReceiver!; 55 | var variadicMethodBuilder = new Dictionary(); 56 | 57 | foreach (var methodDecl in receiver!.MethodsWithAttributes) 58 | { 59 | var model = context.Compilation.GetSemanticModel(methodDecl.SyntaxTree); 60 | var symbol = model.GetDeclaredSymbol(methodDecl); 61 | 62 | if (symbol is IMethodSymbol calledSymbol) 63 | { 64 | var methodSymbol = calledSymbol.OriginalDefinition; 65 | var attr = symbol.GetAttributes().SingleOrDefault(c => 66 | c.AttributeClass!.Name.StartsWith("Induction")); 67 | var name = attr?.ConstructorArguments[0].Value?.ToString(); 68 | var builder = name switch 69 | { 70 | { } when variadicMethodBuilder.TryGetValue(name, out var existing) => existing, 71 | { } => variadicMethodBuilder[name] = new(), 72 | _ => null 73 | }; 74 | switch (attr) 75 | { 76 | case { AttributeClass.Name: "InductionBaseOfAttribute" }: 77 | builder!.Base = (methodSymbol.ReturnType, methodSymbol); 78 | break; 79 | case { AttributeClass.Name: "InductionTransitionOfAttribute" }: 80 | builder!.Transition = methodSymbol; 81 | break; 82 | case { AttributeClass.Name: "InductionFinalizationOfAttribute" }: 83 | builder!.Finalization = (methodSymbol.ReturnType, methodSymbol); 84 | break; 85 | } 86 | } 87 | } 88 | 89 | var calls = new Dictionary<(string Name, Sequence?, int Arity), Induction>(); 90 | 91 | foreach (var invocation in receiver!.Invocations) 92 | { 93 | if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) 94 | if (variadicMethodBuilder.TryGetValue(memberAccess.Name.Identifier.ToString(), out var builder)) 95 | { 96 | var induction = builder.ToInduction(); 97 | Sequence? types = null; 98 | if (!induction.Transition.IsGenericMethod) 99 | { 100 | var model = context.Compilation.GetSemanticModel(invocation.ArgumentList.Arguments[0].SyntaxTree); 101 | var operation = model.GetOperation(invocation); 102 | types = new(operation?.Children.Skip(1).Select(c => c.Type ?? throw new("It's null!!!")) ?? throw new("It's null!!!")); 103 | } 104 | var count = invocation.ArgumentList.Arguments.Count; 105 | calls[(memberAccess.Name.Identifier.ToString(), types, count)] = induction; 106 | } 107 | } 108 | 109 | foreach (var ((name, types, arity), ((baseType, baseMethod), transition, (finalType, finalMethod))) in calls) 110 | { 111 | var classType = baseMethod.ContainingType; 112 | 113 | var typeParameters = 114 | transition.IsGenericMethod 115 | ? ("<" + string.Join(", ", 116 | Enumerable.Range(1, arity) 117 | .Select(x => $"T{x}")) + ">") 118 | : ""; 119 | 120 | var parameters = ""; 121 | if (transition.IsGenericMethod) 122 | { 123 | parameters = string.Join(", ", Enumerable.Range(1, arity).Select(x => $"T{x} value{x}")); 124 | } 125 | else if (types is { } validTypes) 126 | { 127 | parameters = string.Join(", ", validTypes.Seq.Select((x, i) => $"{x.ToDisplayString()} value{i + 1}")); 128 | } 129 | 130 | var steps = 131 | Enumerable.Range(1, arity) 132 | .Select(c => $"value = {transition.Name}(value{c}, value);\n"); 133 | var src = 134 | $@" 135 | partial class {classType.Name} 136 | {{ 137 | public static {finalType.ToDisplayString()} {name}{typeParameters}({parameters}) 138 | {{ 139 | var value = {baseMethod.Name}(); 140 | {string.Join("", steps)} 141 | return {finalMethod.Name}(value); 142 | }} 143 | }} 144 | "; 145 | if (!classType.ContainingNamespace.IsGlobalNamespace) 146 | src = $"namespace {classType.ContainingNamespace}\n{{\n{src}\n}}"; 147 | context.AddSource($"VariadicMethod.{name}.{arity}.cs", src); 148 | } 149 | } 150 | 151 | public void Initialize(GeneratorInitializationContext context) 152 | { 153 | #if DEBUG && false 154 | if (!Debugger.IsAttached) 155 | { 156 | Debugger.Launch(); 157 | } 158 | #endif 159 | context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); 160 | } 161 | 162 | private sealed class SyntaxReceiver : ISyntaxContextReceiver 163 | { 164 | private readonly List _invocations; 165 | private readonly List _methodsWithInductionAttributes; 166 | 167 | public SyntaxReceiver() 168 | { 169 | _invocations = new(); 170 | _methodsWithInductionAttributes = new(); 171 | } 172 | 173 | public IReadOnlyList Invocations 174 | => _invocations; 175 | 176 | public IReadOnlyList MethodsWithAttributes 177 | => _methodsWithInductionAttributes; 178 | 179 | public void OnVisitSyntaxNode(GeneratorSyntaxContext context) 180 | { 181 | if (context.Node is InvocationExpressionSyntax args) 182 | { 183 | _invocations.Add(args); 184 | } 185 | else if (context.Node is MethodDeclarationSyntax method && method.AttributeLists.Count > 0) 186 | { 187 | _methodsWithInductionAttributes.Add(method); 188 | } 189 | } 190 | } 191 | } 192 | --------------------------------------------------------------------------------