CraftTweaker

CraftTweaker

151M Downloads

Method overloading issue

Daomephsta opened this issue ยท 2 comments

commented

Issue Description:

When attempting to overload a ZenMethod, if the overloaded method and the overloading method are only differentiated by the type of an array, the ZenScript engine cannot determine which to use.
I have made a small test mod that reproduces this issue. A compiled binary of this test mod can be found here.

What happens:

When test.zs executes the line TestMethods.test1(["foo", "bar"]); CraftTweaker throws this error:
test.zs:2 > 2 methods available but none matches the parameters (any[])
This is usually an error in your script, not in the mod
test1(ZenTypeArrayBasic )
test1(ZenTypeArrayBasic )
Full minetweaker.log

What you expected to happen:

The overload of TestMethods.test1() with the parameter String[] testString is called.

Affected Versions (Do not use "latest"):

  • Minecraft: 1.10.2
  • Forge: 1.10.2-12.18.1.2064
  • Crafttweaker: 1.10.2-3.0.23
commented

Okay, here on how CraftTweaker determines what type a value is:
If you declare a value without casting it to any type it tries predicting what type you wanted. If it can't (for example because Arrays are harder to predict than single types, it assumes you wanted the ANY type that usually can be converted to the other types.
However, if you overload a method like this, it cannot determine whether it should convert the any[] to a int[] or a string[]

You can overload your methods if you chose to do so, however that would require you/the users to explicitly cast their values to a type

You could execute
TestMethods.test1(["foo","bar"] as string[]); and it would work

However, overloading a method with such similar types might not only confuse the parser, but your users as well, I'd recommend you trying not to do so if not needed

commented

@kindlich
Aah, I see. I forgot that Zenscript is dynamically typed. I'll close this issue then, since it's not really an issue.