English | 日本語 | 简体中文 | 繁體中文 | Esperanto | Klingon | Español | Français | Deutsch | 한국어
PureSharp is a toolset designed to strongly support "referential transparency" and "immutability" in C#, bringing the safety of functional programming to C#. It leverages Roslyn analyzers to enforce robust, bug-resistant code writing at the compilation level.
- Purity Enforcement: Explicitly declare logic without side effects and verify it mechanically.
- Immutability Introduction: Achieve "non-reassignable local variables," a feature lacking in the language specification, through intuitive naming conventions.
- Safe Control Flow: Provide pipeline-style conditional branching to prevent runtime exceptions and oversights.
Provides fundamental attributes and utilities.
[PureMethod]attribute: Declares a method as "pure" (referentially transparent).Fluent.If: A fluent interface that allowsif-elsestatements to be written as expressions.
Monitors code writing in real-time and reports rule violations.
An Error is reported when a method annotated with [PureMethod] performs the following operations:
- Accessing static, mutable (non-readonly) fields.
- Calling non-pure methods (methods without
[PureMethod]). - I/O operations (access to Console, File, Network, etc.).
Achieves immutability using a naming convention that starts with an underscore (_).
- Mandatory Immutability: Prohibits reassignment to local variables starting with
_(Error). - Initialization Enforcement: Variables starting with
_must be initialized at the time of declaration (Error). - Naming Suggestion: Suggests adding an underscore (
_) to variables that have never been reassigned (Warning).
- Verifies that chains starting with
Fluent.Ifare correctly terminated with.Else(). Failure to terminate will result in a compile error.
using PureSharp.Core;
public class Calculator
{
private static int _globalCache; // Mutable static field
[PureMethod]
public int Add(int a, int b)
{
// OK: Calculation only
return a + b;
// NG: Accessing static field causes RT0001 error
// _globalCache = a + b;
// NG: I/O operations cause RT0003 error
// Console.WriteLine(a);
}
}public void Process()
{
int _result = Calculate(); // Declared as an immutable variable
// NG: Attempting to reassign causes LVP0001 error
// _result = 10;
int count = 0; // Regular variable
// Suggestion: If not reassigned, a warning to change to "_count" (LVP0003)
}int status = Fluent.If(score >= 80, () => 1)
.ElseIf(score >= 60, () => 2)
.Else(0); // Forgetting .Else() causes FIF0001 error- PureSharp.Core: Provides core attributes and runtime libraries (.NET 10.0 / netstandard2.0).
- PureSharp.Analyzers: The main Roslyn analyzer (netstandard2.0).
- PureSharp.Analyzers.Tests: Unit tests to verify analyzer behavior (xUnit).
C# is a very powerful language, but in large-scale development or complex logic, debugging can become difficult due to unintended side effects or variable reuse. PureSharp was born to provide developers with "freedom (from bugs)" in the form of "constraints."
This project is released under the MIT License.