Question
Flávio Lúcio Na... · May 6

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
00
1 0 4 165
Log in or sign up to continue

ObjectScript does not have such thing as an Interface.  The only way to get something like this is to use Abstract class, with methods that throw an error, while not overridden.

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()
  }
}

}