Skip to content

Run C# code at compile time!

Compare
Choose a tag to compare
@qwertie qwertie released this 06 Jul 18:50
· 196 commits to master since this release

This release integrates the Roslyn Scripting engine behind C# Interactive, making it much easier to do arbitrary code transformations at compile time. There are a few new macros, most notably compileTime and precompute, which together let you do almost anything. See issue 112 for the design discussion. Here's an example.

compileTime {
	#r "bin\Debug\Loyc.Utilities.dll"

	using System.Collections.Generic;
	var d = new Dictionary<int,string> { [2] = "Two" };

	var stat = new Loyc.Utilities.Statistic();
	stat.Add(5);
	stat.Add(7);
}
public class Class
{
	// Yeah baby.
	const string Two = precompute(d[2]);
	const int Six = precompute((int) stat.Avg());
	double Talk() { 
		precompute(new List<LNode> {
			quote(Console.WriteLine("hello")),
			quote { return $(LNode.Literal(Math.PI)); }
		});
	}
}

Output:

public class Class
{
	// Yeah baby.
	const string Two = "Two";
	const int Six = 6;
	double Talk() {
		Console.WriteLine("hello");
		return 3.14159265358979;
	}
}

2020/07/09: .config files were added to the zip file to fix an error about System.Runtime.CompilerServices.Unsafe.