typelead/eta

FFI example won't run, needs some tweaks...

GordonBGood opened this issue · 3 comments

Description

When trying the language by using the "Object Arrays" example it wouldn't compile due to some missing header declarations

Expected Behavior

Expected it to immediately compile and run successfully.

Actual Behavior

Failure to compile...

Possible Fix

Add/correct the two lines at the beginning of the example:

{-# LANGUAGE MultiParamTypeClasses, DataKinds #-}

import Java hiding (JInteger)

data JInteger = JInteger @java.lang.Integer
  deriving (Class, Show)

data JIntegerArray = JIntegerArray @java.lang.Integer[]
  deriving Class

foreign import java unsafe "@new" toJInteger :: Int -> JInteger
foreign import java unsafe intValue :: JInteger -> Int

-- There's a default instance for object arrays, so no need to define your own.

instance JArray JInteger JIntegerArray
main :: IO ()
main = java $ do
  arr <- arrayFromList integers
  elems <- withObject arr $ mapM aget [0..9]
  io $ print elems
  withObject arr $ mapM_ (\i -> aset i (toJInteger (i * 2))) [0..9]
  arrList <- arr <.> arrayToList
  io $ print arrList
  where integers = map toJInteger [1..10]

Steps to Reproduce

Just tried the example as it exists in the Eta User Guide as follows:

{-# LANGUAGE MultiParamTypeClasses #-}

import Java

data JInteger = JInteger @java.lang.Integer
  deriving (Class, Show)

data JIntegerArray = JIntegerArray @java.lang.Integer[]
  deriving Class

foreign import java unsafe "@new" toJInteger :: Int -> JInteger
foreign import java unsafe intValue :: JInteger -> Int

-- There's a default instance for object arrays, so no need to define your own.

instance JArray JInteger JIntegerArray
main :: IO ()
main = java $ do
  arr <- arrayFromList integers
  elems <- withObject arr $ mapM aget [0..9]
  io $ print elems
  withObject arr $ mapM_ (\i -> aset i (toJInteger (i * 2))) [0..9]
  arrList <- arr <.> arrayToList
  io $ print arrList
  where integers = map toJInteger [1..10]

Context

It was frustrating in starting out because the example didn't work, and had I not had some experience with Haskell, could have me to lose interest in Eta.

Your Environment

Windows 10 64-bit updated other than for the October 2018 Update, and installed and running Eta just using "etlas".

The UX for working with Java Arrays is unsatisfactory and we're currently working on making the interface a lot cleaner in conjunction with a simplified interface to Java, see #647. The types will look like Array Byte instead of the awkward JByteArray.

We need to have a more systematic way of ensuring that the examples are working with changes to Eta. Perhaps we parse the markdown files inside of the docs folder and generate a bunch of files which we then compile.

In the meantime, would you like to contribute the fix to the documentation? The only required change is import Java should change to import Java hiding (JInteger).

I have made a PR #935 with the corrected example, and also added the expected output below the example.