Skip to content

Builder Value Type is not well defined after builder(Class<?> ...) method ... Consider PreBuilders #38

Open
@kaifox

Description

@kaifox

Unfortunately, with the current implementation, it is not possible to create a tensor by writing something like:

Tensor<Double> tensor = Tensorics.builder(C1.class, C2.class).put(at(c1a, c2a), 0.0).build();

The reason for this is that the compiler cannot guess the type of the value after calling the builder(..) method.

Currently, there exist 2 workarounds:

  1. assigning the builder to a local variable, so that the compiler can infer the types:
ImmutableTensor.Builder<Double> builder = Tensorics.builder(C1.class, C2.class);
Tensor<Double> tensor = builder.put(at(c1a, c2a), 0.0).build();
  1. Specifying explicit type arguments:
Tensor<Double> tensor = Tensorics.<Double>builder(C1.class, C2.class).put(at(c1a, c2a), 0.0).build();

Each of them has their specific 'uglyness':
(1) is just annying for tensors with only a few arguments, where the chaining of the methods would be simply convenient ... assigning to a dedicated variable is just boilerplate.
(2) for example does not allow to have a static import to the builer() method ... it always requires the 'Tensorics' class in front ....

Both now become much more prominent, in case we decide to introduce limited support for typed coordinates (#36). In this case in both cases the lines will be polluted by a lot of generics arguments ... so in this cases, it would give more value to work on this problem.

One idea, which also plays in the direction of other issues would be something like explicitly giving the type of the value of a tensor. Imagine something like:

Tensor<Double> tensor = Tensorics.builder(C1.class, C2.class)
                            .valueType(Double.class)
                            .put(at(c1a, c2a), 0.0)
                            .build();

This might not look like a gain at the first glance... however... imagine, we introduce a shortcut, so that the first put(....) method further specifies the value type (lets call the returned object beforehand a 'PreBuilder` for the moment:

Tensor<Double> tensor = Tensorics.builder(C1.class, C2.class) // <PreBuilder<Object>> (or not type parameter at all )
                            .put(at(c1a, c2a), 0.0)  // Builder<Double>
                            .build();

... still does not look impressing, I know ...

However, if we look at other issues we have, for example that we are considering introducing specific tensor implementations for certain types (e.g. preferred dimensions - #11 - or even for certain value types - e.g. double array backed -) ... this could be a potential solution: Whenever the first put() method is called, the PreBuilder manifestates into a builder of an appropriate implementation as best guessed by the arguments given so far (e.g. max number of values, ordering of dimensions ...)....

... just thoughts ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions