2014-04-14 10 views
6

मैं स्कॉटी का उपयोग एक बहुत ही सरल एपीआई बनाने के लिए करने की कोशिश कर रहा हूं। मैं स्कॉटी मोनैड्स का विस्तार करना चाहता हूं जैसे कि मेरे मार्ग हैंडलर क्रियाएं एक अपरिवर्तनीय वातावरण तक पहुंचने में सक्षम हैं। मेरा मानना ​​है कि ऐसा करने का तरीका स्टैक पर Reader मोनैड जोड़ना होगा। अभी के लिए मैं बस कुछ Text डेटा पास करना चाहता हूं।स्कॉटी के मोनड में रीडर मोनड कैसे जोड़ूं?

type BrandyScottyM = ScottyT TL.Text (ReaderT T.Text IO) 
type BrandyActionM = ActionT TL.Text (ReaderT T.Text IO) 

https://github.com/stu-smith/Brandy/blob/0838a63537d7e396ac82d58d460c6529349303d3/src/Core.hs

तो मेरा पहला सवाल है, यह सही तरीका है:

मैं इस प्रकार स्कॉटी monads बढ़ाया है?

मैंने अपने मार्ग हैंडलर के प्रकारों को सफलतापूर्वक बदल दिया है, लेकिन मैं इस स्टैक का उपयोग करके स्कॉटी को लॉन्च करने का तरीका नहीं समझ सकता।

runScotty :: Port -> Text -> BrandyScottyM() -> IO() 
runScotty port file = T.scottyT port ((\f -> runReader f file)) id 

https://github.com/stu-smith/Brandy/blob/0838a63537d7e396ac82d58d460c6529349303d3/src/Main.hs

लेकिन मैं त्रुटि मिलती है:

Couldn't match type `IO' with `Data.Functor.Identity.Identity' 
    Expected type: Reader Text (IO a) 
     Actual type: ReaderT Text IO a 
    In the first argument of `runReader', namely `f' 
    In the expression: runReader f file 
    In the second argument of `T.scottyT', namely 
     `((\ f -> runReader f file))' 
/home/stu/git/Brandy/src/Main.hs: line 36, column 65: 
    Couldn't match type `ReaderT Text IO Network.Wai.Internal.Response' 
        with `IO Network.Wai.Internal.Response' 
    Expected type: ReaderT Text IO Network.Wai.Internal.Response 
        -> IO Network.Wai.Internal.Response 
     Actual type: ReaderT Text IO Network.Wai.Internal.Response 
        -> ReaderT Text IO Network.Wai.Internal.Response 
    In the third argument of `T.scottyT', namely `id' 
    In the expression: T.scottyT port ((\ f -> runReader f file)) id 
    In an equation for `runScotty': 
     runScotty port file = T.scottyT port ((\ f -> runReader f file)) id 

तो मेरी दूसरा सवाल, कैसे मैं स्कॉटी एक अलग इकाई के ढेर के साथ शुरू कर रहा है मैं निम्नलिखित का प्रयास किया है? यह मोनैड ट्रांसफार्मर का उपयोग करने का मेरा पहला प्रयास है और मुझे निराशाजनक रूप से खो दिया प्रतीत होता है।

उत्तर

9

आपका दृष्टिकोण ठीक दिखता है। प्रकार त्रुटि है क्योंकि आपको runReaderTrunReader (runReader के बजाय केवल Reader का उपयोग करने के लिए उपयोग करना चाहिए, जो कि ReaderT है, केवल इसके नीचे डमी Identity मोनैड के साथ)।

+1

आह शानदार मुझे सही लाइनों पर मिला। मुझे एप्लिकेशन और एक्शन स्तर दोनों पर 'रीडर' भी बनाना था: इसलिए अंतिम कॉल है: 'T.scottyT पोर्ट (\' runReaderT \ 'फ़ाइल) (\' runReaderT \ 'फ़ाइल)' – stusmith

संबंधित मुद्दे