ZScript: Return type mismatch (derived type)

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
User avatar
Player701
 
 
Posts: 1649
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

ZScript: Return type mismatch (derived type)

Post by Player701 »

The thread title length limit strikes again. The correct title should be: "ZScript: Return type mismatch when returning an instance of a derived type".

The following ZScript will make GZDoom produce an error message:

Code: Select all

class TestA
{    
}

class TestB : TestA
{
    static TestB Create()
    {
        return new ("TestB");
    }
}

class Test
{
    TestA GetTest()
    {
        return TestB.Create();
    }
}

Code: Select all

Return type Pointer<Class<TestA>> mismatch with Pointer<Class<TestB>>
Script error, "zscript.txt:ZSCRIPT" line 17:
Return type mismatch
This error message is wrong, since a pointer to a TestB is technically also a pointer to a TestA because TestB is a kind of a TestA (i.e. pointer types are covariant to class types). There are currently two ways to work around this:
  1. Replace TestB.Create() with new("TestB"). Interestingly enough, this doesn't produce any errors, though it is not an ideal solution if the class needs to do special initialization (which the class itself should be responsible for).
  2. Cast the return expression to TestA. This also fixes the error.
However, it'd still be better if the example code were allowed to compile - then there wouldn't be a need for any of the above.

Tested on: GZDoom 3.6.0, g3.7pre-182-gb1d35eb0b
You do not have the required permissions to view the files attached to this post.
User avatar
Player701
 
 
Posts: 1649
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: ZScript: Return type mismatch (derived type)

Post by Player701 »

This issue is still present as of GZDoom 4.2.4, and all these casts I have to do still annoy me to no end.

I've tried to fix it with this PR. I'm not 100% sure if I did everything right, will be very glad if someone can review it. But at least it does seem to fix the issue.
_mental_
 
 
Posts: 3814
Joined: Sun Aug 07, 2011 4:32 am

Re: ZScript: Return type mismatch (derived type)

Post by _mental_ »

Merged in 8c05816.

Return to “Closed Bugs [GZDoom]”