1

I have been trying out Frege and one of the first things I would like to do is implement a Java interface.

How is that done?

Here's my example in Java:

package mypkg;

import frege.repl.FregeRepl;
import frege.runtime.Concurrent;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;


public class FregeMain implements BundleActivator {

    public FregeMain() {

    }

    @Override
    public void start( BundleContext context ) throws Exception {
        System.out.println( "Frege Bundle activated" );
        new Thread( () -> FregeRepl.main( new String[ 0 ] ) ).start();
    }

    @Override
    public void stop( BundleContext context ) throws Exception {
        System.out.println( "Frege stopping. Goodbye!" );
        Concurrent.shutDownIfExists();
    }

}

To implement this in Frege, I would need to know:

  • how to declare something that will be visible as a class called mypkg.FregeMain implementing BundleActivator in JVM bytecode (notice that this is important as the OSGi framework will scan the jar for classes implementing that interface, and call them automatically).

  • How to implement a Runnable (as a Haskell lambda, probably) and pass it on to the Thread constructor. Also same issue: implement a Java interface, but this time with an anonymous class or lambda.

I tried to understand the Calling Java from Frege post, but probably due to my lack of experience in Frege/Haskell, I just don't understand most of that.

Thanks for any input.

Renato
  • 12,940
  • 3
  • 54
  • 85
  • Why would you do this? Your Java glue code is short and probably correct? – Ingo Jun 03 '16 at 21:08
  • I wouldn't do this. I just want to learn. – Renato Jun 03 '16 at 22:14
  • I understand. It is so that the concept of "interface" is not really part of Frege. Therefore, your code above should be just fine. Since it does something very Java specific, it is ok if it is in Java. – Ingo Jun 03 '16 at 22:19

1 Answers1

1

The simplest way to implement Java interface in Frege is possibly to use an inline module definition. Some thorough examples are in https://github.com/Frege/FregeFX/blob/master/fregefx/src/main/frege/fregefx/JavaFxUtils.fr

Dierk
  • 1,308
  • 7
  • 13
  • The example looks like just Java code inlined in the frege file. Is this how you do it? Can you show the equivalent code from my sample? – Renato Jun 03 '16 at 20:59
  • 1
    You'd need a native declaration of `data BA = native org.osgi.framework.BundleActivator` and then `native module interface BA where {` your methods here `}` – Ingo Jun 03 '16 at 21:15