ZScript ignores expressions on left side of a static call

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

Moderator: GZDoom 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.

Please make a new topic for every bug. Don't combine multiple bugs into a single topic. Thanks!

ZScript ignores expressions on left side of a static call

Postby Gutawer » Wed Apr 27, 2022 8:34 am

Example code:
Code: Select allExpand view
class T {
    int mem;
    T modify() {
        mem += 1;
        return self;
    }
    static void doit() {
        Console.printf("hi");
    }
}
class Whatever : Actor {
    override void tick() {
        let x = new("T");
        x.modify().doit();
        Console.printf("%d", x.mem);
    }
}

Expected behaviour here is that hi and then 1 is printed, but because doit is a static function, it doesn't require a self pointer, and therefore x.modify() never has code emitted. So it prints hi and then 0 instead.
Another example is that:
Code: Select allExpand view
T x = NULL;
x.doit();

Causes no null pointer problems, because x never gets null checked - although this case is more debatable since languages like Java behave the same here.
User avatar
Gutawer
User Accounts Assistant
 
Joined: 16 Apr 2016
Discord: Gutawer#3431

Re: ZScript ignores expressions on left side of a static cal

Postby Graf Zahl » Wed Apr 27, 2022 10:55 am

How about "Don't do that?" I'd consider that undefined behavior in the best of cases. The problem here is that the expression gets thrown out long before it gets evaluated
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: ZScript ignores expressions on left side of a static cal

Postby Gutawer » Wed Apr 27, 2022 1:20 pm

I completely agree, but in that case, shouldn't it be a compiler error? That's precedented by languages like C#, which only allow calling of static methods using the type name, apart from the one exception of calling it without any prefix (i.e. doSomething() instead of self.doSomething()).
User avatar
Gutawer
User Accounts Assistant
 
Joined: 16 Apr 2016
Discord: Gutawer#3431


Return to Bugs

Who is online

Users browsing this forum: No registered users and 0 guests