CC: Tweaked

CC: Tweaked

57M Downloads

Ability to return Functions from peripherals.

Mathilde411 opened this issue ยท 6 comments

commented

Hey I tried something like that but it didn't work.

@LuaFunction
public final Function<String, String> test() {
    return (t) -> {
        return "Test " + t;
    };
}

So I assume that it is impossible to return functions.
Would it be possible to add such a feature ?
Thanks ^^

commented

I guess it would be possible to allow returning something similar to a ILuaCallback, but one which accepts IArguments. Handling the more generic Function<_, _> (and other functional interfaces like Consumer<_>) is much harder to do.

Just curious as to what the use-case is here? It's not something we've really needed before, so interested to see where it's wanted.

commented

I would use it for defining functions in Prepared SQL request-like system with object oriented lua on a Database peripheral.

commented

Like you'd do:
local r = db.prepareTableCreation()
and then
r.executeRequest()
or
r.setPrimaryKey()
that could verify if a primary key already exists.

commented

If you just need a table of objects, you can just return another object with @LuaFunctions. For instance,

@LuaFunction
public final TableBuilder prepareTableCreation() {
  return new TableBuilder();
}

public static class TableBuilder {
  @LuaFunction
  public final Object executeRequest() { return null;  } 
}
commented

Oh, I didn't know that, thanks I'll definitely test it :)

commented

That's like, exactly what I needed, thanks ^^