3

When using the Frege native-gen tool on the JavaFX Animation class it generates Frege code that includes the following:

data Animation = mutable native javafx.animation.Animation where
  native getRate   :: Animation -> IO Double
  native getStatus :: Animation -> IO Animation_Status

but the code does not compile since the Animation_Status type is missing. On the Java side, this is an enum. http://docs.oracle.com/javafx/2/api/javafx/animation/Animation.Status.html

What is the advised way of handling this in the native declaration?

Dierk
  • 1,308
  • 7
  • 13

1 Answers1

3

We can generate Frege code for inner classes. The class name that is input to the native-gen tool is the name returned by Class.getName. For the Animation.Status enum, it is javafx.animation.Animation$Status.

$ java -jar native-gen-1.0-SNAPSHOT.jar 

Enter class name: javafx.animation.Animation$Status

data Animation_Status = pure native javafx.animation.Animation.Status where

  pure native paused "javafx.animation.Animation.Status.PAUSED" :: Animation_Status
  pure native running "javafx.animation.Animation.Status.RUNNING" :: Animation_Status
  pure native stopped "javafx.animation.Animation.Status.STOPPED" :: Animation_Status

  pure native valueOf "javafx.animation.Animation.Status.valueOf" :: String -> Animation_Status

  native values "javafx.animation.Animation.Status.values" :: () -> STMutable s (JArray Animation_Status)

derive Serializable Animation_Status

By the way, the native-gen version I am working on currently can generate Frege code for an entire Java package and its subpackages recursively. We could just give the root package javafx and it would create Frege modules for all the classes in all its subpackages. I will release this hopefully by the end of this week.

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • 2
    To make things easier for users - have you considered replacing the dots with $ beginning from the right, until either the class is found or no dots are left? I do this in the compiler in frege.compiler.JavaTypes.classForName – Ingo Aug 18 '15 at 18:33
  • As a curious by stander, I'm merely wondering: why can't the Frege compiler auto-generate those types by merely seeing a reference to them at compile time? I know Frege is an entirely different beast from it, but in Scala almost all of the time, one can just use existing JVM types and modules for free without any manual wiring. I'm not saying this should be the case in Frege for all parts of the JVM, but enums are such a simple and pure construct... – Erik Kaplun Aug 19 '15 at 10:48
  • @ErikAllik An enum is not that simple as you may think because it can have methods that can return a value of some type which could be pure or impure that is not known to the Frege compiler. It is different from Scala in that in Frege, side effects are tracked by compiler unlike Scala where you can introduce side effects anywhere. It means that all the methods should be explicit about side effects in Frege. For native types and methods, Frege compiler cannot know and guarantee this on its own. – Marimuthu Madasamy Aug 19 '15 at 16:51
  • I know Haskell so yeah. But methodless enums could still be auto-handled. Or all enums but methods always ignored. – Erik Kaplun Aug 20 '15 at 14:56
  • Furthermore, one has to inspect whether a method might return null (which requires `Maybe`) or throw an RuntimeException and how to handle those. The rigidity that Frege demands when calling into Java allows the language to keep up its type guarantees. – Dierk Aug 20 '15 at 21:04