#Vargs of different types

1 messages · Page 1 of 1 (latest)

dapper rivet
#

Right now the interface looks like this:

        var meta = new BDict(
                new BString("announce"), new BString(announce),
                new BString("created by"), new BString(createdBy),
                new BString("comment"), new BString(comment),
                new BString("encoding"), new BString(encoding),
                new BString("info"), new BDict(
                        new BString("length"), new BInt(length)));
    public BDict(BValue<?>... kv) {

Is there any way I can take either String or Int in vargs and then dispatch correct constructor (either BInt or BString) so this looks something like:

new BDict("announce", announce, "length", 2137)

I looked at stdlib but they do this strange way
https://github.com/openjdk/jdk/blob/8073914af7d4ddd7bbd93d75104c7637e38a7ad9/src/java.base/share/classes/java/util/Map.java#L1623

vivid birchBOT
#

<@&987246399047479336> please have a look, thanks.

shy venture
#

i encountered a very similar problem with my json library

#
import dev.mccue.json.Json;
import dev.mccue.json.JsonObject;

public class Main {
   public static void main(String[] args) {
      JsonObject swedishChef = Json.objectBuilder()
              .put("name", "chef")
              .put("nationality", "swedish")
              .put("lines", 1)
              .build();

      System.out.println(swedishChef);
   }
}
#

where if you don't go through the builder you need

#
        JsonObject.of(Map.of(
                "announce", Json.of("..."),
                "length", Json.of(123)
        ));
#

now seperately

#

having BString.of instead of new BString can help you in the future with binary compatibility

#

also here is that json library: this file has a lot of the conversions, but you can dig through to see more