| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Language.Java.Inline
Description
Inline Java quasiquotation
See the GHC manual for an introduction to quasiquotation. The quasiquoter exported in this module allows embedding arbitrary Java expressions and blocks of statements inside Haskell code. You can call any Java method and define arbitrary inline code using Java syntax. No FFI required.
Here is the same example as in Language.Java, but with inline Java calls:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}
module Object where
import Language.Java as J
import Language.Java.Inline
newtype Object = Object (J ('Class "java.lang.Object"))
instance Coercible Object
clone :: Object -> IO Object
clone obj = [java| $obj.clone() |]
equals :: Object -> Object -> IO Bool
equals obj1 obj2 = [java| $obj1.equals($obj2) |]
...
- java :: QuasiQuoter
Documentation
java :: QuasiQuoter Source #
Java code quasiquoter. Example:
imports "javax.swing.JOptionPane"
hello :: IO ()
hello = do
message <- reflect ("Hello World!" :: Text)
[java| JOptionPane.showMessageDialog(null, $message) |]
A quasiquote is a snippet of Java code. The code is assumed to be a block
(sequence of statements) if the first non whitespace character is a {
(curly brace) character. Otherwise it's parsed as an expression. Variables
with an initial $ (dollar) sign are allowed. They have a special meaning:
they stand for antiqotation variables (think of them as format specifiers in
printf format string). An antiquotation variable $foo is well-scoped if
there exists a variable with the name foo in the Haskell context of the
quasiquote, whose type is Coercible to a Java primitive or reference type.
NOTE: In GHC 8.0.2 and earlier, due to #12778, a quasiquote must always return a boxed value (i.e. an object, not void or a primitive type). This limitation may be lifted in the future.