uCalc API Version: 2.1.3-preview.2 Released: 6/17/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
DefaultClear
Method
Product:
Class:
Resets the default instance stack, restoring the original default instance to its initial state.
Syntax
Parameters
Return
void
No return value.
Remarks
🧹 Resetting the Global uCalc State
The static uCalc.DefaultClear() method provides a powerful way to reset the global uCalc environment. It unwinds the default instance stack and restores the original, built-in default instance to its pristine, startup condition.
⚙️ How the Default Instance Stack Works
uCalc maintains a stack of "default" instances. This allows different parts of an application to temporarily set their own uCalc instance as the active default without losing the previous one. This behavior is LIFO (Last-In, First-Out).
- Initial State: At startup, there is one built-in default instance on the stack. You can access it via
uCalc.GetDefaultInstance(). - Pushing a New Default: When you call
myInstance.IsDefault(true),myInstanceis pushed onto the top of the stack. It becomes the new active default returned byuCalc.GetDefaultInstance(). - Popping an Instance: When an instance that is on the stack is disposed of, or if you call
myInstance.IsDefault(false), it is popped from the stack, and the previous instance becomes the active default again. - Clearing the Stack:
uCalc.DefaultClear()removes all instances from the stack except for the original one. It then resets that original instance, clearing all user-defined variables, functions, and other settings.
🆚 Comparative Analysis: Global State Management
The Traditional Approach (e.g., C# / C++)
In a standard application, managing a global or shared "calculator" object is often done using a Singleton pattern or a simple static class.
// C# Singleton Examplepublic sealed class GlobalCalculator{ private static readonly GlobalCalculator instance = new GlobalCalculator(); private GlobalCalculator() { /* setup */ } public static GlobalCalculator Instance { get { return instance; } } public double Evaluate(string expression) { /* ... */ } public void Reset() { /* ... */ }}// UsageGlobalCalculator.Instance.Evaluate("1+1");While functional, this approach has drawbacks:
- Rigidity: There is only one global instance. If different components need different settings (e.g., custom functions, different number precision), they must manually configure and restore the single instance, which is error-prone.
- State Conflicts: Component A might change a setting that Component B relies on, leading to unexpected behavior.
- Testing: Tightly coupling to a global static instance makes unit testing difficult.
The uCalc Advantage
uCalc's default instance stack provides a more flexible and robust solution. It behaves like a managed, scoped global context.
- Scoped Overrides: A component can push its own configured
uCalcinstance onto the stack, perform its work, and then pop it off, automatically restoring the previous context. This prevents state conflicts. - Centralized Reset: The
uCalc.DefaultClear()method acts as a global "panic button." It's invaluable in scenarios like:- Tear-down logic in unit tests to ensure a clean state for every test run.
- An "application reset" feature for end-users.
- Recovering from an error state where the default stack might be corrupted.
In essence, uCalc provides a structured mechanism for managing a shared resource, avoiding the common pitfalls of global static objects.
Examples
Setting default uCalc instance with uCalc.IsDefault(); also clearing all uCalc instances from default list
using uCalcSoftware;
var uc = new uCalc();
uCalc.DefaultInstance.DefineVariable("val = 'original default'");
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"));
uc.DefineVariable("val = 'uc'");
uc.IsDefault = true;
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"));
var ucB = new uCalc();
ucB.DefineVariable("val = 'ucB'");
ucB.IsDefault = true;
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"));
var ucC = new uCalc();
ucC.DefineVariable("val = 'ucC'");
ucC.IsDefault = true;
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"));
uCalc.DefaultClear();
// The original unnamed default instance is reset so user variable val no longer exists
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"));
// The other instances are removed from Default list but remain active
Console.WriteLine(uc.EvalStr("val"));
Console.WriteLine(ucB.EvalStr("val"));
Console.WriteLine(ucC.EvalStr("val"));
original default
uc
ucB
ucC
Undefined identifier
uc
ucB
ucC using uCalcSoftware; var uc = new uCalc(); uCalc.DefaultInstance.DefineVariable("val = 'original default'"); Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")); uc.DefineVariable("val = 'uc'"); uc.IsDefault = true; Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")); var ucB = new uCalc(); ucB.DefineVariable("val = 'ucB'"); ucB.IsDefault = true; Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")); var ucC = new uCalc(); ucC.DefineVariable("val = 'ucC'"); ucC.IsDefault = true; Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")); uCalc.DefaultClear(); // The original unnamed default instance is reset so user variable val no longer exists Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")); // The other instances are removed from Default list but remain active Console.WriteLine(uc.EvalStr("val")); Console.WriteLine(ucB.EvalStr("val")); Console.WriteLine(ucC.EvalStr("val"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::DefaultInstance().DefineVariable("val = 'original default'");
cout << uCalc::DefaultInstance().EvalStr("val") << endl;
uc.DefineVariable("val = 'uc'");
uc.IsDefault(true);
cout << uCalc::DefaultInstance().EvalStr("val") << endl;
uCalc ucB;
ucB.DefineVariable("val = 'ucB'");
ucB.IsDefault(true);
cout << uCalc::DefaultInstance().EvalStr("val") << endl;
uCalc ucC;
ucC.DefineVariable("val = 'ucC'");
ucC.IsDefault(true);
cout << uCalc::DefaultInstance().EvalStr("val") << endl;
uCalc::DefaultClear();
// The original unnamed default instance is reset so user variable val no longer exists
cout << uCalc::DefaultInstance().EvalStr("val") << endl;
// The other instances are removed from Default list but remain active
cout << uc.EvalStr("val") << endl;
cout << ucB.EvalStr("val") << endl;
cout << ucC.EvalStr("val") << endl;
}
original default
uc
ucB
ucC
Undefined identifier
uc
ucB
ucC #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::DefaultInstance().DefineVariable("val = 'original default'"); cout << uCalc::DefaultInstance().EvalStr("val") << endl; uc.DefineVariable("val = 'uc'"); uc.IsDefault(true); cout << uCalc::DefaultInstance().EvalStr("val") << endl; uCalc ucB; ucB.DefineVariable("val = 'ucB'"); ucB.IsDefault(true); cout << uCalc::DefaultInstance().EvalStr("val") << endl; uCalc ucC; ucC.DefineVariable("val = 'ucC'"); ucC.IsDefault(true); cout << uCalc::DefaultInstance().EvalStr("val") << endl; uCalc::DefaultClear(); // The original unnamed default instance is reset so user variable val no longer exists cout << uCalc::DefaultInstance().EvalStr("val") << endl; // The other instances are removed from Default list but remain active cout << uc.EvalStr("val") << endl; cout << ucB.EvalStr("val") << endl; cout << ucC.EvalStr("val") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
uCalc.DefaultInstance.DefineVariable("val = 'original default'")
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"))
uc.DefineVariable("val = 'uc'")
uc.IsDefault = true
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"))
Dim ucB As New uCalc()
ucB.DefineVariable("val = 'ucB'")
ucB.IsDefault = true
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"))
Dim ucC As New uCalc()
ucC.DefineVariable("val = 'ucC'")
ucC.IsDefault = true
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"))
uCalc.DefaultClear()
'// The original unnamed default instance is reset so user variable val no longer exists
Console.WriteLine(uCalc.DefaultInstance.EvalStr("val"))
'// The other instances are removed from Default list but remain active
Console.WriteLine(uc.EvalStr("val"))
Console.WriteLine(ucB.EvalStr("val"))
Console.WriteLine(ucC.EvalStr("val"))
End Sub
End Module
original default
uc
ucB
ucC
Undefined identifier
uc
ucB
ucC Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() uCalc.DefaultInstance.DefineVariable("val = 'original default'") Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")) uc.DefineVariable("val = 'uc'") uc.IsDefault = true Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")) Dim ucB As New uCalc() ucB.DefineVariable("val = 'ucB'") ucB.IsDefault = true Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")) Dim ucC As New uCalc() ucC.DefineVariable("val = 'ucC'") ucC.IsDefault = true Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")) uCalc.DefaultClear() '// The original unnamed default instance is reset so user variable val no longer exists Console.WriteLine(uCalc.DefaultInstance.EvalStr("val")) '// The other instances are removed from Default list but remain active Console.WriteLine(uc.EvalStr("val")) Console.WriteLine(ucB.EvalStr("val")) Console.WriteLine(ucC.EvalStr("val")) End Sub End Module