• 🏆 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!

Optional libs

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
I was thinking of adding Table as an optional lib for one of my systems.

However I don't quite get how to make it work.
I can use
JASS:
static if(LIBRARY_Table)
{
    //do something table related
}

But I want to declare a Table variable inside my struct which seems impossible with my limited knowledge.

JASS:
//! zinc
library x requires optional Table
{
	struct y
	{
		Table t; //<<<<
		static method create() -> thistype
		{
			thistype this = thistype.allocate();
			static if(LIBRARY_Table)
			{
				//do something table related
			}
			return this;
		}
	}
}
//! endzinc
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
So I can have a static if outside a method/function? :eek:
JASS:
//! zinc
library x requires optional Table
{
    struct y
    {
		static if(LIBRARY_Table)
		{
			Table t; //does not work
		}
        static method create() -> thistype
        {
            thistype this = thistype.allocate();
            static if(LIBRARY_Table)
            {
                 Table t = Table.create(); // works but it's local
            }
            return this;
        }
    }
}
//! endzinc

Because moving the declaration into the other if would make the variable local.
 
No it does not make it local.

If the "then" part of the "if" runs, then it's accessable in the whole scope, and it was declared as global. It will be fine.

You can wrap anything inside your if statement, any code you want. If the condition is not met, then the code will never exist, and other way around.

You maybe also can do crazy shit like this:

JASS:
static if(LIBRARY_Rebel) then
    function I_AM_A_REBEL_FUNCTION takes nothing returns nothing
else
    function I_AM_NO_REBEL_FUNCTION takes nothing returns nothing
endif
        // function body
    endfunction
 
Level 17
Joined
Dec 11, 2014
Messages
2,004
You'll need another struct with the same members of table.
JASS:
library x uses optional Table
    struct FalseTable
    //Table members here

    //Never looked at table closely, I honestly don't know how to use it
    //Use locals to set instance members here.

    endstruct

    struct y
        FalseTable t
        static method sth takes nothing returns nothing
            set t = FalseTable.create()
            //use t as table
        endmethod
    endstruct
endlibrary

Can be done in the same struct but it'll get messy.
 
If you remove the static if, then it works?

I can't test it, but I fail to see a mistake actually..;o

Static if works like a normal if, but it is evaluated during compile time already, not in runtime.

So it just says if a part of code gets compiled, or not..

static if (true)
this code gets compiled
else
this code gets not compiled
endif

And if the library exists then LIBRARY_Table will return true and should work just normaly.

You need to test around. :p
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
Okay it has to do with zinc somehow.
JASS:
library y
	struct x
		static if LIBRARY_Table then
			 Table t
		endif
	endstruct
endlibrary

//! zinc
	library yy{
		struct xx{
			static if(LIBRARY_Table){
				 Table t;
			}
		}
	}
//! endzinc
93248f85a395cce2ece064aef558c81b.png
 
Level 7
Joined
Oct 19, 2015
Messages
286
zinc does not support static ifs outside functions/methods. It's quite annoying, I have no idea why Vex thought this would be better.

Optional is really only needed for public code so one solution is to not bother developing public resources. Another option is to just force your requirements on users (it's your library anyway, if you want to use Table then use it). A third option is to constantly use typecasting between integer and struct. A fourth option is to use vJass instead of zinc for all libraries that need static ifs outside functions.
 
Status
Not open for further replies.
Top