Utility Functions

Metaprogramming

DolphinDB supports generating metacode for method calls defined within classes using the following functions:

  • objCall(obj, methodName, args...)

  • unifiedObjCall(obj, methodName, args)

  • makeObjCall(obj, methodName, args...)

  • makeUnifiedObjCall(obj, methodName, args)

where

  • obj is the object.

  • methodName is a string representing the method name.

  • args... are the parameters passed to the method.

Examples

Use makeObjCall to create metacode for OOP method calls:

class Employee {
  name :: STRING
  salary :: DOUBLE
  department :: STRING
  
  def Employee(name_, salary_, dept_) { 
    name = name_
    salary = salary_
    department = dept_
  }
  
  def updateInfo(newSalary,newDepartment) {
    salary = newSalary
    department = newDepartment
    return "Salary updated to $" + string(salary) + ", and department updated to " + department
  }
  
  def getInfo(includeDetails) {
    return iif(includeDetails, 
               name + " - " + department + " - $" + string(salary),
               name + " - " + department)
  }
}
emp = Employee("Alice", 50000.0, "Department1")

// Use objCall
objCall(emp, "getInfo", true)
// Use unifiedObjCall
unifiedObjCall(emp, "updateInfo", (55000.0,"Department2"))
// Use makeObjCall
makeObjCall(emp, "updateInfo", 60000.0,"Department3").eval()
// Use makeUnifiedObjCall
makeUnifiedObjCall(emp, "updateInfo", (65000.0,"Department4")).eval()

Other Functions

  • isInstanceOf(obj, cls): Checks if an object is an instance of a specific class or its derived class, returning a Boolean value.

    class B {
      def B() {}
    }
    class D : B {
      def D() {}
    }
    class A {
      def A() {}
    }
    d = D()
    isInstanceOf(d, D)  // true
    isInstanceOf(d, B)  // true
    isInstanceOf(d, A)  // false
  • setAttr(obj, attrName, value): Sets or modifies the attributes of an object obj. For example, setAttr(obj, `alpha, 16), or obj.setAttr(`alpha, 16).

These utility functions enable flexible and dynamic manipulation of class instances and their methods, enhancing the power of object-oriented programming in DolphinDB.