HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Properties

06-05-2009, 08:30 PM#1
cohadar
Quote:
Originally Posted by Vexorian
I plan to make onDestroy get called at the moment the index is recycled, though I need to make some changes to jasshelper first...

How about instead of making unnecessary hacks to jasshelper in order to implement a feature that noone needs and noone asked for you do something usefull.

For example find a way for me to declare properties without using textmacros.

Collapse JASS:
//===========================================================================
//  Allowed PII_PROPERTY TYPES are: item, integer, real, boolean, string
//  Do NOT put handles that need to be destroyed here (timer, trigger, ...)
//  Instead put them in a struct and use PII textmacro
//===========================================================================
//! textmacro PII_PROPERTY takes VISIBILITY, TYPE, NAME, DEFAULT
$VISIBILITY$ struct $NAME$
    private static item   array pii_item
    private static $TYPE$ array pii_data
    
    //-----------------------------------------------------------------------
    //  Returns default value when first time used
    //-----------------------------------------------------------------------
    static method operator[] takes item whichItem returns $TYPE$
        local integer pii = GetItemIndex(whichItem)
        if .pii_item[pii] != whichItem then
            set .pii_item[pii] = whichItem
            set .pii_data[pii] = $DEFAULT$
        endif
        return .pii_data[pii]
    endmethod
    
    //-----------------------------------------------------------------------
    static method operator[]= takes item whichItem, $TYPE$ whichData returns nothing
        local integer pii = GetItemIndex(whichItem)
        set .pii_item[pii] = whichItem
        set .pii_data[pii] = whichData
    endmethod
endstruct
//! endtextmacro

Note that property is a pure static struct with parameters, basically a verbatim text copy without side effects, so it should be easier than full struct parameter support.

And it would be much more useful than a stupid module.
06-05-2009, 10:29 PM#2
Vexorian
I can't find any good use for properties. For attaching stuff to units or items, you need two things, either simple tagging or things that requires managing. For simple tags, just GetItemId and indexing is more than enough. For complicated things, being able to do cleanup (onDestroy) is better. With this logic, properties become a hybrid that is too costly (textmacros) for simple stuff, and still costly (textmacros + a whole new struct per 'property' ) yet insufficient (lack of onDestroy) for complex things.

Calling onDestroy at the second it is removed is not that necessary (as it will be called when necessary and is enough to prevent leaks), but being able to use this as a remove event listener would be cool, not a priority though. I don't remember what change to jasshelper I needed for it to work, I'll probably need to do the change eventually.

If people need that stuff they can always use that PII stuff. This system does not need to monopolize UserData, so it is perfectly possible to have both things in the same place.
06-06-2009, 12:22 AM#3
cohadar
I was not talking only in terms of PUI and PII.

Lets analize what property really is:
It is a static structs only because structs provide operator overloading for [] and []=
So creating a struct is not really needed, just a language construct that is able to:

1. declare arrays
2. declare functions that work with those arrays

And being able to declare arrays is the key feature here, lets face it, the whole reason we have bizzare language constructs like modules and properties is because jass has no dynamic memory.

So basically I am asking for a general memory allocation problem solution.
Collapse JASS:
public property PII
    private item array pii_item
    private TYPE array pii_data
    
    //-----------------------------------------------------------------------
    public operator[] takes item whichItem returns TYPE
        local integer pii = GetItemIndex(whichItem)
        if .pii_item[pii] != whichItem then
            set .pii_item[pii] = whichItem
            set .pii_data[pii] = DEFAULT
        endif
        return .pii_data[pii]
    endoperator
    
    //-----------------------------------------------------------------------
    public operator[]= takes item whichItem, TYPE whichData returns nothing
        local integer pii = GetItemIndex(whichItem)
        set .pii_item[pii] = whichItem
        set .pii_data[pii] = whichData
    endoperator

    //-----------------------------------------------------------------------
    public function SomeFunc takes trigger trig, TYPE whichData returns boolean
        // add something here
    endfunction
        
endproperty

So TYPE and DEFAULT are reserved words inside property but you need to specify only one because DEFAULT can be deducted from TYPE
0 for integer, 0.0 for real, null for handles
(NULL would be a better name for DEFAULT i think)

And the best thing is you would declare it like this:
Collapse JASS:
declare PII as killCounter<integer>
So as you can see the only parameter is TYPE which is great because you can check at compile time if user specified an actuall jass type, no raw text formatting here.
06-06-2009, 03:11 AM#4
Vexorian
Well, I was reading this from a netbook so I applied pattern matching on your post and assumed its contents...

Now I am at a real computer. Is this really necessary? Seems a little too specific in application right now and the syntax varies too much...

Reusing an old idea, which I think you've seen before:
Collapse JASS:
struct PII takes type T

     static method operator []= takes item it returns T
             //do stuff
     endmethod

endstruct

type goo extends PII(integer)
06-06-2009, 08:15 AM#5
cohadar
Yes much better, reusing parts of syntax tree = always good idea.

With one small addition:
Collapse JASS:
static struct PII takes type T

     static method operator []= takes item it returns T
             //do stuff
     endmethod

endstruct

type goo extends PII(integer)


Maybe the functioning of this can be best described through error messages:
Code:
* static structs must have type parameter
* static structs can have only static variables
* static structs can have only static methods
* static structs cannot be created/destroyed
* cannot use static struct directly, must instantiate it first

The only thing that is not clear to me is should you be able to do things like this:
Collapse JASS:
static struct PII takes type T

     static method operator []= takes item it returns T
             //do stuff
     endmethod

endstruct

type goo extends PII(integer)

// Data would be responsible for creating it's own maintainance arrays
// but would have the ability to use goo's static variables and methods
struct Data extends goo
endstruct

But when you thing about it, it is just unnecessary complication,
static structs have no use for extending into normal structs because you can simply use them directly from normal struct. (has a in this case is the same as is a)

EDIT:
actually when you think about it, the support for normal static structs should be added first and then the support for parametrized ones.
static struct & static type ?