Open ErzengelLichtes opened 5 years ago
ZS does not allow for functions as parameters because internally it uses different types. This would only be possible if you were to use a function that is registered to ZS as a functional Interface (e.g. IItemCondition)
Short of forking CT, can we register a functional interface using ZenClasses?
Take this example (the 2nd function parameter for recipes)
So it's like using lambda needs an interface? If we are allowed to define interfaces or function signatures, it seems this is possible.
You can use lambdas when you have registered a functional interface using Java Code. You cannot create functional interfaces from within ZS, but you can implement them (that is what a lambda is)
Hey there,
I apologize for the late reply. Currently I have a rudimentary implementation for function types as parameters and members in my local dev environment.
var myTestFun as function(string)void = function(s as string) as void {print(s);};
myTestFun('Hello World');
function myAcceptFun(consumer as function(string)void) as void {
consumer('hello from inside myAcceptFun');
}
myAcceptFun(myTestFun);
myAcceptFun(function(s as string) as void {print('Another thing: ' ~ s);});
Would print
Hello World
hello from inside myAcceptFun
Another thing: hello from inside myAcceptFun
And
zenClass myClass {
var consumer as function(string)void;
zenConstructor(consumer as function(string)void) {
this.consumer = consumer;
}
function acceptString(x as string) as void {
consumer(x);
}
}
myClass(function(x as string) as void {print('inside fun: ' ~ x);}).acceptString('abc');
Would print
inside fun: abc
Creating a custom function that accepts a function causes a java.lang.ArrayIndexOutOfBoundsException
Example Script
Exception in crafttweaker.log
CraftTweaker version: CraftTweaker2-1.12-4.1.15