ZScript new function parameters support

Remember, just because you request it, that doesn't mean you'll get it.

Moderator: GZDoom Developers

ZScript new function parameters support

Postby Cacodemon345 » Fri Jul 24, 2020 11:47 am

Use case for this would be to clean up code and efficiently initialize objects. Right now, initializing a class and initializing its members requires a lot of code. This feature would allow removing such code.
Cacodemon345
 
Joined: 22 Dec 2017
Discord: Cacodemon345#9151
Github ID: Cacodemon345
Operating System: Other Linux 64-bit
Graphics Processor: ATI/AMD (Modern GZDoom)

Re: ZScript new function parameters support

Postby m8f » Fri Jul 24, 2020 12:15 pm

Can you please make an example of "a lot of code" and how do you imagine "new function parameters support" would look if implemented?
User avatar
m8f
dreamer
 
 
 
Joined: 29 Dec 2017
Location: Siberia (UTC+7)
Discord: m8f#0629
Github ID: mmaulwurff
Operating System: Debian-like Linux (Debian, Ubuntu, Mint, etc) 64-bit

Re: ZScript new function parameters support

Postby Cacodemon345 » Fri Jul 24, 2020 12:22 pm

Code: Select allExpand view
class ExmpClass
{
int firstInt;
int secondInt;
double doubleValue;
// With a constuctor
ExmpClass(int firstParam,int secondParam, double thirdParam)
{
firstInt = firstParam;
secondInt = secondParam;
doubleValue = thirdParam;
}
}
auto newExmpClass = new("ExmpClass",0,5,0.5); // Which is something I imagine
// As of now what needs to be done.
auto newExmpClass = new("ExmpClass");
ExmpClass.firstInt = 0;
ExmpClass.secondInt = 5;
ExmpClass.doubleValue = 0.5;
There's of course new'ing them and then calling a defined function in them for initializations, but I still would like to see this feature.
Cacodemon345
 
Joined: 22 Dec 2017
Discord: Cacodemon345#9151
Github ID: Cacodemon345
Operating System: Other Linux 64-bit
Graphics Processor: ATI/AMD (Modern GZDoom)

Re: ZScript new function parameters support

Postby m8f » Fri Jul 24, 2020 7:29 pm

Below is two alternatives to object initialization (makeshift constructors), which you can use with existing syntax:

Code: Select allExpand view
class ExmpClass
{

  int firstInt;
  int secondInt;
  double doubleValue;

  ExmpClass init(int firstParam, int secondParam, double thirdParam)
  {
    firstInt = firstParam;
    secondInt = secondParam;
    doubleValue = thirdParam;

    return self;
  }

  static
  ExmpClass makeNew(int firstParam, int secondParam, double thirdParam)
  {
    let result = new("ExmpClass");

    result.firstInt = firstParam;
    result.secondInt = secondParam;
    result.doubleValue = thirdParam;

    return result;
  }
}

let newExmpClass1 = new("ExmpClass").init(0, 5, 0.5);
let newExmpClass2 = ExmpClass.makeNew(0, 5, 0.5);


Maybe you'll find one of them more convenient that by direct field initialization.

I also think that proper constructors in ZScript would be neat, but I believe that it's not that can be easily added, otherwise it would be already in.
User avatar
m8f
dreamer
 
 
 
Joined: 29 Dec 2017
Location: Siberia (UTC+7)
Discord: m8f#0629
Github ID: mmaulwurff
Operating System: Debian-like Linux (Debian, Ubuntu, Mint, etc) 64-bit


Return to Feature Suggestions

Who is online

Users browsing this forum: No registered users and 0 guests