Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Varargs in functions #36

Open
PrincessRTFM opened this issue Jun 8, 2020 · 2 comments
Open

Varargs in functions #36

PrincessRTFM opened this issue Jun 8, 2020 · 2 comments

Comments

@PrincessRTFM
Copy link

Looking at the CraftTweaker documentation for IOreDictEntry, the example for adding items indicates that varargs functions exist, as shown here:

//oreDictEnt.add(IItemStack... item_items);
oreDictEnt.add(<minecraft:grass>);
oreDictEnt.add(<minecraft:iron_ore>, <minecraft:dirt>);

However, nowhere in the documentation for custom functions is there any indication of it being possible to declare a custom function to take a varargs parameter. I tried, as a quick test, declaring a function parameter parameterName as string... (and it was not just the last but the only parameter in that function) but running /ct syntax gave me the error ) expected at that line. I also tried declaring parameterName... as string[] and got the same error.

Can custom functions be declared to take varargs parameters? If so, what is the syntax (and can it be added to the documentation)? If not, can this be implemented?

Final note: I felt this belonged in the ZenScript issue tracker because it's a feature inherent to the language, not the minecraft-specific application of it. If I'm wrong about that, I can open a copy of this on the CraftTweaker issue tracker instead.

@PrincessRTFM
Copy link
Author

Unrelated side note, I love that github markdown syntax highlighting understands zenscript as a language.

@kindlich
Copy link
Contributor

kindlich commented Jun 8, 2020

ZS supports the idea of varargs.
However, currently only methods that are declared in the Java Code allow for varargs.

Custom functions in ZS are still pretty limited. They do not allow for varargs.
Heck, normal functions don't even allow for overloading or optional parameters.

If you are looking on how to use varargs when in Java Code, you can see here
https://github.com/CraftTweaker/CraftTweaker/blob/1.12/CraftTweaker2-API/src/main/java/crafttweaker/api/oredict/IOreDictEntry.java#L24

Since varargs are just syntactic sugar for an array creation, you can create the array explicitly.
Or you could create a zenClass inside ZS code, and have overloads for the most cases, so something like

zenClass MyClass {
    zenConstructor(){}
    function doSomething() as void {doSomething([]);}
    function doSomething(arg0 as string) as void {doSomething([arg0]);}
    function doSomething(arg0 as string, arg1 as string) as void {doSomething([arg0, arg1]);}
    function doSomething(arg0 as string, arg1 as string, arg2 as string) as void {doSomething([arg0, arg1, arg2]);}
    function doSomething(arg0 as string, arg1 as string, arg2 as string, arg3 as string) as void {doSomething([arg0, arg1, arg2, arg3]);}
    function doSomething(varargs as string[]) as void{
        /*Code here*/
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants