Internal Test: A variadic function that sums only the arguments matching a specific data type name, testing argument introspection capabilities.

ID: 1244

				
					using uCalcSoftware;

var uc = new uCalc();

static void SumIfType(uCalc.Callback cb) {
   var typeName = cb.ArgStr(1);
   double total = 0;
   string totalStr = "";
   var i = 0;

   // Loop through all arguments starting from the second one.
   for ( i = 2; i <= cb.ArgCount(); i++) {
      var item = cb.ArgItem(i);
      // Check if the argument's data type name matches.
      if (item.DataType.Name == "double") {
         total = total + item.Value();
      } else if (item.DataType.Name == "string") {
         totalStr = totalStr + item.ValueStr();
      }
   }

   if (typeName == "double") {
      totalStr = total.ToString();
   }
   cb.ReturnStr(totalStr);
}

// Define the variadic function.
uc.DefineFunction("SumIfType(typeName As String, ByHandle other As AnyType ...) As String", SumIfType);

// This call will sum only the double values (5.0 and 10.123456), ignoring the integer.
Console.WriteLine(uc.EvalStr("SumIfType('double', 5.0, 'Hello ', 10.123456, 'world!')"));

// This call will concatinate only the string values.
Console.WriteLine(uc.EvalStr("SumIfType('string', 5.0, 'Hello ', 10.123456, 'world!')"));
				
			
15.123456
Hello world!
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call SumIfType(uCalcBase::Callback cb) {
   auto typeName = cb.ArgStr(1);
   double total = 0;
   string totalStr = "";
   auto i = 0;

   // Loop through all arguments starting from the second one.
   for ( i = 2; i <= cb.ArgCount(); i++) {
      auto item = cb.ArgItem(i);
      // Check if the argument's data type name matches.
      if (item.DataType().Name() == "double") {
         total = total + item.Value();
      } else if (item.DataType().Name() == "string") {
         totalStr = totalStr + item.ValueStr();
      }
   }

   if (typeName == "double") {
      totalStr = to_string(total);
   }
   cb.ReturnStr(totalStr);
}
int main() {
   uCalc uc;
   // Define the variadic function.
   uc.DefineFunction("SumIfType(typeName As String, ByHandle other As AnyType ...) As String", SumIfType);

   // This call will sum only the double values (5.0 and 10.123456), ignoring the integer.
   cout << uc.EvalStr("SumIfType('double', 5.0, 'Hello ', 10.123456, 'world!')") << endl;

   // This call will concatinate only the string values.
   cout << uc.EvalStr("SumIfType('string', 5.0, 'Hello ', 10.123456, 'world!')") << endl;
}
				
			
15.123456
Hello world!
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub SumIfType(ByVal cb As uCalc.Callback)
      Dim typeName = cb.ArgStr(1)
      Dim total As Double = 0
      Dim totalStr As String = ""
      Dim i = 0
      
      '// Loop through all arguments starting from the second one.
      For i  = 2 To cb.ArgCount()
         Dim item = cb.ArgItem(i)
         '// Check if the argument's data type name matches.
         If item.DataType.Name = "double" Then
            total = total + item.Value()
            ElseIf item.DataType.Name = "string" Then
            totalStr = totalStr + item.ValueStr()
         End If
      Next
      
      If typeName = "double" Then
         totalStr = total.ToString()
      End If
      cb.ReturnStr(totalStr)
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define the variadic function.
      uc.DefineFunction("SumIfType(typeName As String, ByHandle other As AnyType ...) As String", AddressOf SumIfType)
      
      '// This call will sum only the double values (5.0 and 10.123456), ignoring the integer.
      Console.WriteLine(uc.EvalStr("SumIfType('double', 5.0, 'Hello ', 10.123456, 'world!')"))
      
      '// This call will concatinate only the string values.
      Console.WriteLine(uc.EvalStr("SumIfType('string', 5.0, 'Hello ', 10.123456, 'world!')"))
   End Sub
End Module
				
			
15.123456
Hello world!