· May 6, 2021

Interface using Caché

Hello everyone,


I have a doubt, its possible to use interface like C# using COS? 

Remember, interface is different from Abstract Class, because a abstract class can implement code in the method, so I don't want this, I want only define the methods from Class, not allowing implement code.

Description of C# interface: An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. Beginning with C# 8.0, an interface may define a default implementation for members.  

Link: interface - C# Reference | Microsoft Docs

Product version: IRIS 2020.1
Discussion (5)2
Log in or sign up to continue

Well, there are few ways to do it. Look at the description to methods

Class Abstract.Test [ Abstract ]

/// Will return status with error, if the method not implemented
ClassMethod DoAction(pActionName) As %Status
  Quit $$$ERROR($$$GeneralError, "DoAction not implemented by " _ ..%ClassName(1))

/// Will throw an exception when method called, while not implemented 
ClassMethod DoCall(pActionName)
  Throw ##class(%Exception.General).%New("DoCall not implemented by " _ ..%ClassName(1))

/// Will not allow to compile descendants without implementing the method
ClassMethod AnotherAction(pActionName) As %Status [ CodeMode = objectgenerator ]
  If ('%class.Abstract) {
    Quit $$$ERROR($$$GeneralError, %method.Name _ " not implemented by " _ %classname)
  Quit $$$OK

ClassMethod TestCall()
  Try {
    Set tSC = ..DoAction()
    If $$$ISERR(tSC) {
      Do $System.OBJ.DisplayError(tSC)
    Do ..DoCall()
  Catch ex {
    Write !,"Caught exception of type: ",ex.%ClassName(1)
    Write !,"Message: ",ex.DisplayString()