Demonstrates `ByExpr` to create a custom `Assert` function where the error message is only evaluated if the assertion fails, improving performance.

ID: 1249

				
					using uCalcSoftware;

var uc = new uCalc();

static void Assert(uCalc.Callback cb) {
   var condition = cb.ArgBool(1);

   // If the condition is false, then we evaluate the message expression
   if (condition == false) {
      var errorMessage = cb.ArgExpr(2);
      Console.WriteLine($"Assertion failed: {errorMessage.EvaluateStr()}");
   }
}

uc.DefineVariable("x = 50");

// The message is passed as an unevaluated expression
uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", Assert);

// This will do nothing because the condition is true
uc.Eval("Assert(10 < 20, 'This will not be evaluated')");

// This will trigger the assertion and evaluate the message expression
uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')");
				
			
Assertion failed: x (50) is not greater than 100
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call Assert(uCalcBase::Callback cb) {
   auto condition = cb.ArgBool(1);

   // If the condition is false, then we evaluate the message expression
   if (condition == false) {
      auto errorMessage = cb.ArgExpr(2);
      cout << "Assertion failed: " << errorMessage.EvaluateStr() << endl;
   }
}
int main() {
   uCalc uc;
   uc.DefineVariable("x = 50");

   // The message is passed as an unevaluated expression
   uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", Assert);

   // This will do nothing because the condition is true
   uc.Eval("Assert(10 < 20, 'This will not be evaluated')");

   // This will trigger the assertion and evaluate the message expression
   uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')");
}
				
			
Assertion failed: x (50) is not greater than 100
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub Assert(ByVal cb As uCalc.Callback)
      Dim condition = cb.ArgBool(1)
      
      '// If the condition is false, then we evaluate the message expression
      If condition = false Then
         Dim errorMessage = cb.ArgExpr(2)
         Console.WriteLine($"Assertion failed: {errorMessage.EvaluateStr()}")
      End If
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("x = 50")
      
      '// The message is passed as an unevaluated expression
      uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", AddressOf Assert)
      
      '// This will do nothing because the condition is true
      uc.Eval("Assert(10 < 20, 'This will not be evaluated')")
      
      '// This will trigger the assertion and evaluate the message expression
      uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')")
   End Sub
End Module
				
			
Assertion failed: x (50) is not greater than 100