Search code examples
ghchaskell-ffi

Why am I getting parse error on input "{"


I am trying to run the very first example in this tutorial:

Call JVM Methods from Haskell

module Main where

{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -fplugin=Language.Java.Inline.Plugin #-}
import Language.Java (withJVM)
import Language.Java.Inline

main :: IO ()
main = withJVM [] [java| { System.out.println("Hello Java!"); } |]

and I get this error:

app\Main.hs:8:26: error: parse error on input `{'
  |
8 | main = withJVM [] [java| { System.out.println("Hello Java!"); } |]
  |                          ^

What am I doing wrong?


Solution

  • The {-# LANGUAGE … #-} and {-# OPTIONS_GHC … #-} pragmas need to be defined before the module Main declaration. Otherwise it will not enable the QuasiQuotes language extension, and thus not understand the quasiquotes used in the program.

    If you install the inline-java and put the language pragmas before the module Main:

    {-# LANGUAGE QuasiQuotes #-}
    {-# OPTIONS_GHC -fplugin=Language.Java.Inline.Plugin #-}
    
    module Main where
    
    import Language.Java (withJVM)
    import Language.Java.Inline
    
    main :: IO ()
    main = withJVM [] [java| { System.out.println("Hello Java!"); } |]
    

    It should normally interpret the quasiquotes properly.