Java array in Silver

cooper

(Benoît Bousquet) #1

My Silver/Cooper project generates a JAR file. It uses Swift [] arrays (i.e. [Byte]) but I’d prefer if the Java output used native Java arrays (i.e. Byte[] instead of ArrayList<Byte>). What would be the most efficient way to obtain this?


(marc hoffman) #2

You can use both:

[Foo] is a Swift array — a class. Foo[] is a platform-native array (a range of bytes in memory). See https://docs.elementscompiler.com/API/StandardTypes/Arrays/ for more details.


(Benoît Bousquet) #3

Yet if I switch my method declaration (in the Silver/Cooper JAR) to:

func writeData(_ value: java.lang.Byte[])

Java sees it as:

void writeData(byte[] value)

How do I get it to generate:

void writeData(Byte[] value)

It’s as if the type didn’t map properly from Silver to Java. My main reason for wanting this is the fact that the app using my JAR file doesn’t need to know about Elements. I want the exposed classes to only use Java native types.


(marc hoffman) #4

Hmm, that’s a separate issue though. TBH I’m not too familiar with Byte vs byte on Java, and how the compiler handles it exactly. maybe the array types always need to use Byte? I’ll have to defer to @ck to answer this one…

The important part WRT your original question is that you get Byte[], not [Byte]


(Carlo Kok) #5

You’ll want Byte![] in Swift to get a java.lang.Byte[] in Java.


(Benoît Bousquet) #6

On a somewhat related note, if:

Consumer<java.lang.Byte[]> in my Silver/Cooper JAR becomes Consumer<byte[]> in Java

then why does:

Consumer<java.lang.Byte> in my Silver/Cooper JAR become Consumer<Byte> in Java?