SORU
30 EKİM 2013, ÇARŞAMBA


Nasıl düzgün-derleme bilgileri Haskell fonksiyonları Şablon iletişim kurmak için?

Şablon Haskell içine derleme betikleri bazı bilgileri iletmek istiyorum. Şu anda komut sistemi ortamında bilgiyi muhafaza derleme, sadece System.Environment.getEnvironment runIO sarılmış kullanarak okuyun. Daha iyi bir yol, ghc (C -D... benzer bir ön-işlemci), ya da belki de özellikle ekonomik anlamda, bu amaç için tasarlanmış bazı argümanları iletme gibi var mı?

CEVAP
1 Ocak 2014, ÇARŞAMBA


Bu kadar çok insan meselesi ile ilgilenen beri, benim şimdiki yaklaşım daha ekleyeyim, belki birisi yararlı olacaktır. Muhtemelen en iyi yolu TH eğer DZD komut satırında -D parametreleri okumak için izin verilecek, ama bu şu anda uygulanan hiçbir şey gibi görünüyor.

Basit bir modül İNCİ derleme zamanı ortamı okumanızı sağlar. Yardımcı fonksiyon da dosyaları okumak için izin verir; çevreden örneğin bir yapılandırma dosyası yolu okumak ve sonra da dosyayı okuyun.

{-# LANGUAGE TemplateHaskell #-}
module THEnv
    (
    -- * Compile-time configuration
      lookupCompileEnv
    , lookupCompileEnvExp
    , getCompileEnv
    , getCompileEnvExp
    , fileAsString
    ) where

import Control.Monad
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Language.Haskell.TH
import Language.Haskell.TH.Syntax (Lift(..))
import System.Environment (getEnvironment)

-- Functions that work with compile-time configuration

-- | Looks up a compile-time environment variable.
lookupCompileEnv :: String -> Q (Maybe String)
lookupCompileEnv key = lookup key `liftM` runIO getEnvironment

-- | Looks up a compile-time environment variable. The result is a TH
-- expression of type @Maybe String@.
lookupCompileEnvExp :: String -> Q Exp
lookupCompileEnvExp = (`sigE` [t| Maybe String |]) . lift <=< lookupCompileEnv
    -- We need to explicly type the result so that things like `print Nothing`
    -- work.

-- | Looks up an compile-time environment variable and fail, if it's not
-- present.
getCompileEnv :: String -> Q String
getCompileEnv key =
  lookupCompileEnv key >>=
  maybe (fail $ "Environment variable "    key    " not defined") return

-- | Looks up an compile-time environment variable and fail, if it's not
-- present. The result is a TH expression of type @String@.
getCompileEnvExp :: String -> Q Exp
getCompileEnvExp = lift <=< getCompileEnv

-- | Loads the content of a file as a string constant expression.
-- The given path is relative to the source directory.
fileAsString :: FilePath -> Q Exp
fileAsString = do
  -- addDependentFile path -- works only with template-haskell >= 2.7
  stringE . T.unpack . T.strip <=< runIO . T.readFile

Bu gibi kullanılabilir:

{-# LANGUAGE TemplateHaskell #-}
import THEnv
main = print $( lookupCompileEnvExp "DEBUG" )

Sonra:

  • runhaskell Main.hs 10*; *yazdırır
  • DEBUG="yes" runhaskell Main.hs Just "yes" yazdırır.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • FlippinWindows | #1 Windows Tutorial Channel!

    FlippinWindo

    23 Mayıs 2010
  • krotoflik

    krotoflik

    26 ŞUBAT 2011
  • spyib

    spyib

    9 Ocak 2007