Inline Objective-C in Haskell for GHC 7.8
nslog :: String -> IO ()
nslog msg =
$(objc ['msg :> ''String]
(void [cexp| NSLog(@"Here is a message from Haskell: %@", msg) |]))
The latest GHC release (GHC 7.8) includes significant changes to Template Haskell, which make it impossible to get type information from variables in the current declaration group in a Template Haskell function. Version 0.6 of language-c-inline introduces marshalling hints in the form of type annotations to compensate for that lack of type information.
In the above code example, hints are used in two places: (1) the quoted local variable msg
carries an annotation suggesting to marshal it as a String
and (2) the result type of the inline Objective-C code is suggested to be IO ()
by the void
annotation. These hints are required as Template Haskell no longer propagates the type information contained in the function signature.