A practical example of `ByHandle` to create a `TypeOf` function that introspects an argument and returns its data type name as a string.
ID: 1250
using uCalcSoftware;
var uc = new uCalc();
static void GetTypeOf(uCalc.Callback cb) {
// Get the Item object for the argument
var item = cb.ArgItem(1);
// Get the item's DataType, then its name, and return it as a string
cb.ReturnStr(item.DataType.Name);
}
// The ByHandle modifier passes the argument's metadata (Item) instead of its value
uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", GetTypeOf);
uc.DefineVariable("myInt As Int = 10");
uc.DefineVariable("myStr As String = 'hello'");
uc.DefineVariable("myDbl = 3.14"); // Type is inferred as double
Console.WriteLine($"Type of myInt: {uc.EvalStr("TypeOf(myInt)")}");
Console.WriteLine($"Type of myStr: {uc.EvalStr("TypeOf(myStr)")}");
Console.WriteLine($"Type of myDbl: {uc.EvalStr("TypeOf(myDbl)")}");
Type of myInt: int
Type of myStr: string
Type of myDbl: double using uCalcSoftware; var uc = new uCalc(); static void GetTypeOf(uCalc.Callback cb) { // Get the Item object for the argument var item = cb.ArgItem(1); // Get the item's DataType, then its name, and return it as a string cb.ReturnStr(item.DataType.Name); } // The ByHandle modifier passes the argument's metadata (Item) instead of its value uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", GetTypeOf); uc.DefineVariable("myInt As Int = 10"); uc.DefineVariable("myStr As String = 'hello'"); uc.DefineVariable("myDbl = 3.14"); // Type is inferred as double Console.WriteLine($"Type of myInt: {uc.EvalStr("TypeOf(myInt)")}"); Console.WriteLine($"Type of myStr: {uc.EvalStr("TypeOf(myStr)")}"); Console.WriteLine($"Type of myDbl: {uc.EvalStr("TypeOf(myDbl)")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call GetTypeOf(uCalcBase::Callback cb) {
// Get the Item object for the argument
auto item = cb.ArgItem(1);
// Get the item's DataType, then its name, and return it as a string
cb.ReturnStr(item.DataType().Name());
}
int main() {
uCalc uc;
// The ByHandle modifier passes the argument's metadata (Item) instead of its value
uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", GetTypeOf);
uc.DefineVariable("myInt As Int = 10");
uc.DefineVariable("myStr As String = 'hello'");
uc.DefineVariable("myDbl = 3.14"); // Type is inferred as double
cout << "Type of myInt: " << uc.EvalStr("TypeOf(myInt)") << endl;
cout << "Type of myStr: " << uc.EvalStr("TypeOf(myStr)") << endl;
cout << "Type of myDbl: " << uc.EvalStr("TypeOf(myDbl)") << endl;
}
Type of myInt: int
Type of myStr: string
Type of myDbl: double #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call GetTypeOf(uCalcBase::Callback cb) { // Get the Item object for the argument auto item = cb.ArgItem(1); // Get the item's DataType, then its name, and return it as a string cb.ReturnStr(item.DataType().Name()); } int main() { uCalc uc; // The ByHandle modifier passes the argument's metadata (Item) instead of its value uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", GetTypeOf); uc.DefineVariable("myInt As Int = 10"); uc.DefineVariable("myStr As String = 'hello'"); uc.DefineVariable("myDbl = 3.14"); // Type is inferred as double cout << "Type of myInt: " << uc.EvalStr("TypeOf(myInt)") << endl; cout << "Type of myStr: " << uc.EvalStr("TypeOf(myStr)") << endl; cout << "Type of myDbl: " << uc.EvalStr("TypeOf(myDbl)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub GetTypeOf(ByVal cb As uCalc.Callback)
'// Get the Item object for the argument
Dim item = cb.ArgItem(1)
'// Get the item's DataType, then its name, and return it as a string
cb.ReturnStr(item.DataType.Name)
End Sub
Public Sub Main()
Dim uc As New uCalc()
'// The ByHandle modifier passes the argument's metadata (Item) instead of its value
uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", AddressOf GetTypeOf)
uc.DefineVariable("myInt As Int = 10")
uc.DefineVariable("myStr As String = 'hello'")
uc.DefineVariable("myDbl = 3.14") '// Type is inferred as double
Console.WriteLine($"Type of myInt: {uc.EvalStr("TypeOf(myInt)")}")
Console.WriteLine($"Type of myStr: {uc.EvalStr("TypeOf(myStr)")}")
Console.WriteLine($"Type of myDbl: {uc.EvalStr("TypeOf(myDbl)")}")
End Sub
End Module
Type of myInt: int
Type of myStr: string
Type of myDbl: double Imports System Imports uCalcSoftware Public Module Program Public Sub GetTypeOf(ByVal cb As uCalc.Callback) '// Get the Item object for the argument Dim item = cb.ArgItem(1) '// Get the item's DataType, then its name, and return it as a string cb.ReturnStr(item.DataType.Name) End Sub Public Sub Main() Dim uc As New uCalc() '// The ByHandle modifier passes the argument's metadata (Item) instead of its value uc.DefineFunction("TypeOf(ByHandle arg As AnyType) As String", AddressOf GetTypeOf) uc.DefineVariable("myInt As Int = 10") uc.DefineVariable("myStr As String = 'hello'") uc.DefineVariable("myDbl = 3.14") '// Type is inferred as double Console.WriteLine($"Type of myInt: {uc.EvalStr("TypeOf(myInt)")}") Console.WriteLine($"Type of myStr: {uc.EvalStr("TypeOf(myStr)")}") Console.WriteLine($"Type of myDbl: {uc.EvalStr("TypeOf(myDbl)")}") End Sub End Module