如何在弗雷格的非纯本机函数中声明可变类型的 Maybe?

How do I declare Maybe of a mutable type in an non-pure native function in Frege?

native-gen 工具为 showOpenDialog javafx.stage.FileChooser 中的方法像这样

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

编译导致消息

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

现在设置

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

导致

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

但遵循建议会再次出现第一条错误消息。

编译器接受IOMutable File作为return类型,这是有道理的 因为它是一个 IO 操作,return 是一个可变类型。

如果可能,应修改编译器错误消息以避免用户方面感到沮丧。

但是,在这种特殊情况下,文件可以为空等 核心类型不是File而是Maybe File。 但是随后仅使用 IOMutable (Maybe File) 会导致相当令人惊讶的消息

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

关于如何正确声明此类型有什么建议吗?

native-gen生成的代码是错误的,因为File已经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 定义为 type IOMutable d = IO (MutableIO d)。 对于您的情况,可变本机类型 (MutableIO d) 可以为空,因此以下内容应该有效:

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