2

The native-gen tool generates the native declaration for the showOpenDialog method in javafx.stage.FileChooser like so

data FileChooser = mutable native javafx.stage.FileChooser where
 native showOpenDialog :: FileChooser -> Window -> IO File

Compiling leads to the message

Non pure native type File must be MutableIO
    File in IO actions.

Now setting

native showOpenDialog :: FileChooser -> Window -> MutableIO File

leads to

FileChooser.showOpenDialog has an illegal
    return type for a method that is not pure, perhaps ST s (MutableIO
    File) would work

but following the advice leads to first error message again.

The compiler accepts IOMutable File as return type, which makes sense since it is an IO action that returns a mutable type.

If possible, the compiler error message should be adapted to avoid frustration on the user side.

However, in this special situation, the file can be null and so the core type is not File but Maybe File. But then just using IOMutable (Maybe File) leads to the rather surprising message

The type MutableIO (Maybe File) is illegal,
    Maybe File must be a native type.

Any advice on how to properly declare this type?

Dierk
  • 1,308
  • 7
  • 13
  • Note that the original message did tell you the correct solution: replace `File` with `MutableIO File`. Unfortunately, the line break appears in an unfortunate position. Anyway, the message did **not** tell you to remove the `IO`! – Ingo Aug 20 '15 at 16:20
  • True, even though I did not even consider this as a possible meaning of the message :-) Anyway, future readers of this post will profit. – Dierk Aug 20 '15 at 21:14

1 Answers1

2

The code generated by native-gen is wrong because File has been declared as IO in native-gen but File is actually defined as a stateful (not IO) native type as can be seen from here.

IOMutable is defined as type IOMutable d = IO (MutableIO d). For your case, the mutable native type (MutableIO d) can be null so the following should work:

data FileChooser = mutable native javafx.stage.FileChooser where
  native showOpenDialog :: FileChooser -> Window -> IO (Maybe (MutableIO File))
Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • Perhaps the type of File should be changed. It is debatable, to say the least, whether the few pure functions justifiy it being only ST – Ingo Aug 23 '15 at 10:33