[Added] [PR] ZScript abstract functions

Moderator: GZDoom Developers

[PR] ZScript abstract functions

Postby Player701 » Sat Oct 10, 2020 9:04 am

ZScript abstract functions

PR and technical info for developers

Have you ever designed an abstract class in ZScript? If so, you've probably written code that looks like this:

Code: Select allExpand view
class MyAbstractClass abstract
{
    virtual void MyFunc()
    {
    }
   
    virtual int MySecondFunc()
    {
        return 0;
    }
   
    virtual bool MyThirdFunc()
    {
        return false;
    }
}

When you inherit from such a class, it is sometimes easy to forget to override some of those virtuals. This can cause bugs in your code that may be difficult to detect. One possible solution is to put calls to ThrowAbortException in your virtual functions. Now if you forget to override a function, you'll immediately get a VM abort when you try to call it:

Code: Select allExpand view
class MyAbstractClass abstract
{
    virtual void MyFunc()
    {
        ThrowAbortException("%s: Must override MyFunc.", GetClassName());
    }
   
    virtual int MySecondFunc()
    {
        ThrowAbortException("%s: Must override MySecondFunc.", GetClassName());
        return 0;
    }
   
    virtual bool MyThirdFunc()
    {
        ThrowAbortException("%s: Must override MyThirdFunc.", GetClassName());
        return false;
    }
}

And of course this will work. But notice that there is a lot of extra code now. And you have to write it all by hand, or devise some sort of automation tool to insert ThrowAbortException calls in your base virtuals. And of course there may be typos, or bugs in your automation scripts, so the problem isn't solved completely. Though this is probably the best you can come up with... for now.

But soon, not anymore.

Enter abstract functions.

What is an abstract function? Well, programmers already know, of course. But let me explain for those who are new to programming languages. Assuming you know what a virtual function is, an abstract function is just a virtual function without any code:

Code: Select allExpand view
class MyAbstractClass abstract
{
    abstract void MyFunc();
   
    abstract int MySecondFunc();   
   
    abstract bool MyThirdFunc();
}

An abstract function is, by definition, just a declaration, so it does not have a body. Additionally, abstract functions have the following restrictions:

  • Must be defined in an abstract class.
  • Any non-abstract subclass must override them.
Now you see what I'm talking about? If you forget to override something, you will get an error at compile time. This helps reduce the amount of potential bugs in your code without the need to check for errors manually. A net win!
User avatar
Player701
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: [PR] ZScript abstract functions

Postby Player701 » Mon Oct 19, 2020 12:33 am

I thought I'd wait another year for this, but the PR has been merged! So I guess this can be closed now. Thanks Graf!
User avatar
Player701
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Closed Feature Suggestions

Who is online

Users browsing this forum: No registered users and 0 guests