Closed mb720 closed 8 years ago
It is possible to use java.util.Optional
(and any other JDK object) under the condition that you provide a JSweet implementation of it in the j4ts project. Currently j4ts supports basic collections of java.util
but it would be quite simple to add support for Optional
. I encourage everybody to contribute to that project whenever they need a Java API in JSweet.
This could be a nice feature indeed! In addition, it should be fairly simple to implement Optional into j4ts. @mb720, let me know if you PR it, I will give it a try. Thanks.
Thanks for your quick answers!
I'm trying to figure out how to provide a JSweet implementation for Optional
.
In j4ts/target/ts/bundle.ts
starting at line 964 I can see that OptionalDouble
has a TypeScript implementation. Does that mean that I can transpile OptionalDouble
with JSweet 1.1.1?
Because when I try to use OptionalDouble
in my current setup with the JSweet transpiler 1.1.0 I can't: cannot find name 'java' ...
I should have checked. Optional
is indeed already supported by j4ts: https://github.com/cincheo/j4ts/blob/master/src/main/java/java/util/Optional.java.
So the only logical explanation is a problem in the j4ts candy or that you don't have the j4ts candy (or not the right version) in your dependencies. Check your pom.xml
.
<dependency>
<groupId>org.jsweet.candies</groupId>
<artifactId>j4ts</artifactId>
<version>0.1.0</version>
</dependency>
You can also try the 0.2.0-SNAPSHOT
.
Can you check this out?
That looks promising! Indeed, I hadn't included the j4ts candy in my dependencies.
When I do so by adding
compile group: "org.jsweet.candies", name: "j4ts", version: "0.1.0"
to my build.gradle
, JSweet generates a bundle.js
that includes code for java.util.Optional
in .jsweet\candies\js\j4ts-0.1.0
at the root of my project.
How do I change the directory where this bundle.js
is generated so I can source it inside my HTML?
I assumed that this part of the JSweet config in build.gradle
would accomplish that:
jsweet {
outDir = new File('src/main/resources/public/js')
// ...
}
Thanks again for your help so far!
I think you need to use the candiesJsOut
option (see the doc).
Oh thanks, I forgot about candiesJsOut
. It did the trick.
So, in summary, to use java.util.Optional
with JSweet, I had to add
compile group: "org.jsweet.candies", name: "j4ts", version: "0.1.0"
and
jsweet {
// Your path may vary
def jsDir = new File('src/main/resources/public/js')
outDir = jsDir
candiesJsOut = jsDir
// ...
}
to my build.gradle
.
In my HTML I have this:
<body>
// Order of scripts is important since the second script depends on the first
<script src="/js/j4ts-0.1.0/bundle.js"></script>
<script src="/js/FileThatUsesOptional.js"></script>
</body>
Thanks again for your help!
Hi and thanks a lot for JSweet!
I'd like to use
java.util.Optional
or something equivalent in my Java code and transpile it to JavaScript.When I
import java.util.Optional
I get this error:Is there a way to use Optional and have JSweet transpile it?
For context, this is from my
build.gradle
: