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
classorstructthat 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.
Comments
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.
Thanks Dmitry.
Hi @Dmitry Maslennikov
You could give me a example, of how throw an error while method is 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()
}
}
}I believe it doesn't. The interfaces are used as a palliative for the lack of multiple inheritance that the COS has.