1

I'm building a small haste project where I want to use Elasticsearch. However, bloodhound which seems like the library to go for for elasticsearch in haskell depends indirectly on template-haskell - which isn't supported by haste. Now, I don't need to call elastic from the client, so I don't need bloodhound in haste, but I need to be able to call it from within the same code base as haste is built to use the same code for server and client side. I guess I somehow could have separate client and server side implementations but I really like the haste way.

How can I have calls to dependencies that only exist on the server side in haste?

Community
  • 1
  • 1
worldsayshi
  • 1,788
  • 15
  • 31

2 Answers2

3

Preprocessor can be used for this purpose. Haste defines __HASTE__ macro so it should be enough to wrap your code in conditional statement:

{-# LANGUAGE CPP #-}

main = do
#ifdef __HASTE__
    print "haste!"
#endif

#ifndef __HASTE__
    print "not haste!"
#endif

    print "everybody"

Don’t forget to enable C preprocessor extension using {-# LANGUAGE CPP #-} pragma.

You can also achieve similar effect in your ‘.cabal’ file:

Build-Depends:
    bytestring >= 0.9.2.1
if flag(haste-inst)
    Build-Depends:
        base == 4.6.0.1,
        array == 0.4.0.1
else
    Build-Depends:
        base,
        array,
        random,
        websockets >= 0.8

(source https://github.com/valderman/haste-compiler/blob/0.4/libraries/haste-lib/haste-lib.cabal#L63)

Note that the haste-inst flag has been renamed to haste-cabal in the latest development version of Haste.

Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
0

A potential solution I've thought about is to import a "Shared" module with to different implementations, one client/Shared.hs and one server/Shared.hs and then include one of the implementations using the -i option. So -iclient for haste and -iserver for ghc.

I can't test this at the moment though so I'll have to get back to it.

worldsayshi
  • 1,788
  • 15
  • 31
  • This is probably possible as an alternative. I can't currently imagine a scenario where it is preferable to the one I accepted though. – worldsayshi May 17 '15 at 10:20