ZScript: Return type mismatch (derived type)

Is there something that doesn't work right in the latest GZDoom? Post about it here.

Moderator: Developers

Forum rules
Please construct and post a simple demo whenever possible for all bug reports. Please provide links to everything.

If you can include a wad demonstrating the problem, please do so. Bug reports that include fully-constructed demos have a much better chance of being investigated in a timely manner than those that don't.

ZScript: Return type mismatch (derived type)

Postby Player701 » Mon Nov 05, 2018 9:13 am

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 allExpand view
class TestA
{   
}

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

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


Code: Select allExpand view
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
Attachments
zscript.txt
(211 Bytes) Downloaded 5 times
User avatar
Player701
You are now breathing manually.
 
Joined: 13 May 2009
Location: Russian Federation
Discord: Player701#8214

Return to Bugs

Who is online

Users browsing this forum: No registered users and 1 guest