#2659 Fantom Equivalent to Java Code

dal Fri 20 Oct 2017


I'm new in Fantom and wrapping up next java libraries in pod:


There is portion of Java I'm not sure how to write down equivalent in Fantom!

final SparkEvent[] event = {null};
eventStream = device.eventStream(e -> event[0] = e);

Any help is appreciate!

SlimerDude Fri 20 Oct 2017

Hi dal!

It kinda depends on how the device.eventStream() method is defined, but I imagine the code looks a little like this:

// this is a field
SparkEvent?[] events := SparkEvent?[null]


// a statement that passes an event handler function that sets the first item in the list
eventStream := device.eventStream |SparkEvent e| { events[0] = e }

But I'd imagine it'd be better to define a list that takes non-null items and just add the event to the list:

SparkEvent[] events := SparkEvent[,]


eventStream := device.eventStream |SparkEvent e| { events.add(e) }

You may also be able to use an it-block for the event handler:

SparkEvent[] events := SparkEvent[,]


eventStream := device.eventStream { events.add(it) }

dal Sat 21 Oct 2017


Thanks for replay!

I can't make this work.

This is original method!

public SparkEventStream eventStream(Consumer<SparkEvent> eventHandler){
    Client client = ClientBuilder.newBuilder().register(SseFeature.class).build();
    WebTarget target = client.target(session.baseUrl).path("/v1/devices/" + deviceId + "/events/").queryParam("access_token", session.getTokenKey());
    return new SparkEventStream(target, eventHandler);

It is using Consumer Interface that I'm not familiar!

SlimerDude Sun 22 Oct 2017

I can't make this work.

Without an error message, stacktrace, or the fully qualified names of the classes involved, I can't really suggest much.

But, assuming you mean the new java.util.function.Consumer interface of Java 8, then unfortunately I don't think you can use it in Fantom...

For if you try to create an implementation in Fantom, then you get a compilation error:

class MyConsumer : Consumer {
    override Void accept(Obj? obj) { }

    override Consumer? andThen(Consumer? arg) { null }

Class 'MyConsumer' must be abstract since it inherits but doesn't override 

lambda$andThen$0() seems to be a synthetic method related to the new Java8 default methods. But due to it being an illegal method name, it can't be overridden.

So you may need to drop into Java to use that method.

I don't know if Brian, or anyone else, can give any insight in to what these methods are and how they may be used / implemented in Fantom. My concern is that, given Java8 will continue to become more wide spread, these default methods will only become more popular.

brian Sun 22 Oct 2017

You probably aren't going to be use some of the Java 8 language features in Fantom FFI since it was designed before Java 8 came out.

But you can still pretty easily use integrate with anything in Java with native methods - just requires dropping down to Java directly and creating an appropriate wrappers. See docLang::Natives

dal Mon 23 Oct 2017

Thanks for help, you are right, this library is using all new Java 8 features. It looks for now I will not need that method! I will check Natives as well down the road I will rewrite directly in Fantom as practice!

Login or Signup to reply.