• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Override struct.create() method

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,337
Hi,

I think I saw this somewhere in the forums but someone had written static method create takes nothing returns struct or something like that, which inside had code that initialized the values of the struct.

Is this legal? What is the right way to override the struct.create() method (e.g. general syntax, restrictions?). Can .create() take any parameters?

JASS:
struct myStruct
...
static method create takes ... returns myStruct
...
endmethod
...
endstruct

Thank you.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
struct myStruct extends array

Then you create your own create method.
no thats wrong.

To your question: Yes, its legal. To define a custom constructor you can just define a static "create" method with the struct type as return type. You can use any parameters you want. If you dont define a create method a default one will be generated.
However there is one thing you have to do if you define your own constructor, you have to call "thistype.allocate()" and return its return value.

Example:
JASS:
struct MyStruct
    unit myUnit

    static method create takes player p returns thistype
        local thistype this = thistype.allocate()
        set this.myUnit = CreateUnit(p, 'hfoo', 0., 0., 0.)
        return this
    endmethod
endstruct
 
Last edited by a moderator:
Level 15
Joined
Aug 7, 2013
Messages
1,337
no thats wrong.

To your question: Yes, its legal. To define a custom constructor you can just define a static "create" method with the struct type as return type. You can use any parameters you want. If you dont define a create method a default one will be generated.
However there is one thing you have to do if you define your own constructor, you have to call "thistype.allocate()" and return its return value.

Example:
JASS:
struct MyStruct
    unit myUnit

    static method create takes player p returns thistype
        local thistype this = thistype.allocate()
        set this.myUnit = CreateUnit(p, 'hfoo', 0., 0., 0.)
        return this
    endmethod
endstruct

This worked fine, but when I created another struct which extended this one, I wasn't able to make the child's create method.

JASS:
struct parent
  ...
  static method create takes args returns thistype
    ...
  endmethod
  ...
endstruct
...
...
...
struct child extends parent
  ...
  static method create takes args returns thistype
     local thistype this = thistype.allocate() //this line gives an error, "not enough arguments passed"
     ...
  endmethod
  ...
endstruct
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
So child structs must have the same exact args to their custom create...

no, the constructors may have different arguments, only in the derived class (subclass) you have to call ".allocate(...)" with the argumentlist of the superclasses ".create(...)" method. The reason for this is that internally the ".allocate(...)" of the subclass calls the ".create(...)" method of the superclass.

This works just fine:
JASS:
// Superclass:
struct MyStruct
    integer a

    static method create takes integer a returns thistype
        local thistype this = thistype.allocate()
        set this.a = a
        return a
    endmethod
endstruct

// Derived class:
struct MyStructSum extends MyStruct
    static method create takes integer x, integer y returns thistype
        local integer sum = x + y
        local thistype this = thistype.allocate(sum)
        return this
    endmethod
endstruct
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
no thats wrong.
However there is one thing you have to do if you define your own constructor, you have to call "thistype.allocate()" and return its return value.

that is also not accurate.

JASS:
static method create takes nothing returns thistype
    local thistype t = someotherMethod()
    //some stuff
    return t
endmethod

in your words this shouldnt compile, but it does.

You dont generally need to call .allocate method, but you need to return instance of the type the method is in.

why does everyone always use thistype. and this. =|

I rly don't get it >,<

local thistype this = allocate()
set myUnit = CreateUnit(...)
return this

well, some people write .x = ... .y = ... etc, some people like me prefer to write this. and thistype. to explicitly specify what I want to call for users reading and also for the JassHelper

Also you must specify thistype keywords in natives that take code so why not do it everywhere

Its just matter of opinion
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
About implicit thisype/this, i personnaly find ugly as hell, at least we should use the "." to make the difference between struct members and the other ones.
Especially since you can give the same name for a local variable and a struct member.
Also (v)Jass is clearly verbose, implicit stuff is not really a part of it.

And yes, as it has been said, it's just a matter of opinion anyway.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
that is also not accurate.

JASS:
static method create takes nothing returns thistype
    local thistype t = someotherMethod()
    //some stuff
    return t
endmethod

in your words this shouldnt compile, but it does.

You dont generally need to call .allocate method, but you need to return instance of the type the method is in.

Technically ur right, but the only reason not to use the default allocator is to abuse the struct syntax for someting else (like the RGBA system). But for learning structs you should really stick to the basics and use structs how they are supposed to be used (only my opinion...).

About implicit thisype/this, i personnaly find ugly as hell, at least we should use the "." to make the difference between struct members and the other ones.
Especially since you can give the same name for a local variable and a struct member.
Also (v)Jass is clearly verbose, implicit stuff is not really a part of it.

I fully agree.
 
Status
Not open for further replies.
Top