uCalc API Version: 2.1.3-preview.2 Released: 6/16/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.

Clone

Method

Product: 

Fast Math Parser

Class: 

Expression

Creates an identical, yet completely independent, copy of a parsed expression object.

Syntax

Clone()

Parameters

[None]

Return

Expression

A new, independent Expression object that is a complete replica of the original.

Remarks

The Clone method creates a deep, independent copy of a parsed Expression object. This is a highly efficient operation, as it duplicates the internal Abstract Syntax Tree (AST) directly in memory, which is significantly faster than re-parsing the original expression string.

🎯 Core Use Cases

Cloning a parsed expression is essential for several advanced scenarios:

  • Performance Optimization: The most common use case. Parsing a complex expression string can be computationally expensive. If you need to evaluate the same logic repeatedly (e.g., inside a loop or across different parts of your application), you should parse it once with uCalc.Parse, and then Clone the resulting Expression object as needed. This avoids the high cost of repeated parsing.

  • Preserving an Original Template: You can treat a parsed expression as a master template. If you need to experiment with it (e.g., with potential future APIs that might modify the expression tree), you can work on a clone, leaving the original intact.

Expression.Clone() vs. uCalc.Clone()

It is important to understand the distinction between cloning an expression and cloning the entire uCalc engine:

  • Expression.Clone(): Copies a single parsed expression. The clone still relies on the original uCalc instance to resolve variables and functions during evaluation.
  • uCalc.Clone(): Copies the entire engine, including all variables, functions, operators, and settings. The cloned engine is a fully isolated sandbox.

💡 Best Practices for Memory Management

A cloned Expression object, like one created with Parse, holds onto memory and should be released when no longer needed. This can be done explicitly with Release() or automatically using language-specific scoping constructs like using in C# or Owned in C++, as shown in the examples.

Examples