Skip to content

Java interop - consider builders or overloads #222

Description

Hey, thanks for the nice work. I'm trying to use lets-plot in java.

It works well:

var xs = List.of(0, 0.5, 1, 2);
var ys = List.of(0, 0.25, 1, 4);
final Map<?, ?> data = Map.of("x", xs, "y", ys);

final Function1<? super GenericAesMapping, Unit> mapping = aes -> {
  aes.setX("x");
  aes.setY("y");
  return Unit.INSTANCE;
};
var l = ggplot(data, mapping)
    .plus(new geomPoint(null,
        new StatOptions(StatKind.IDENTITY, Options.Companion.empty()),
        new PosOptions(PosKind.IDENTITY, Options.Companion.empty()),
        true, null, null, null, null, null, null, null, null, null, null, null, null, null,
        null, null, null, null,
        aes -> Unit.INSTANCE));
l.show();
ggsave(l, "plot.png", 2.0, null, null);

But the code is pretty heavy because the lib uses a lot of default parameter values and it results in Java in a single full signature, with all parameters present and all default values lost.
This in Java:

new geomPoint(null,
        new StatOptions(StatKind.IDENTITY, Options.Companion.empty()),
        new PosOptions(PosKind.IDENTITY, Options.Companion.empty()),
        true, null, null, null, null, null, null, null, null, null, null, null, null, null,
        null, null, null, null,
        aes -> Unit.INSTANCE)

is the same as

geomPoint()

in Kotlin.

Setting the same default values manually is prone to error and writing series of nulls is annoying.

Any chance we could consider using @JvmOverloads or generate builders?

I gave a try to @JvmOverloads, it works fine for some simple cases but it quickly gets limited as soon as a few parameter needs to be set. For instance, aes is the last parameter in geomPoint and often needs to be set, so I end up having to use the full signature.
So I guess builders are a better solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions