使用snaplet-session输入错误 [英] Type error using snaplet-session

查看:128
本文介绍了使用snaplet-session输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序类型

 数据应用程序=应用程序
{_heist :: Snaplet(Heist应用程序)
,_session :: Snaplet SessionManager
}

初始化程序

  ... 
addRoutes [(/ ss,companyHandler)
,(,heistServe)
]
...

处理程序

  companyHandler :: Handler bv()
companyHandler = method GET getter< |>方法POST setter
其中
getter = do
value< - getFromSessionname
writeText $ fromMaybenothingvalue
setter = do
mname < - getParamname
setInSessionname(convert mname)
getter
convert = T.pack。 B.unpack。 (fromMaybenothing)

heistServe 有类型处理程序b(Heist b)()



类型错误:

$ p $ s code $ s $ / $ Tutorial $ $ $
$`
`v'是一个'Session'刚性类型变量由
绑定companyHandler :: Handler bv()的类型签名src / Tutorial.hs中的
:50:1
预期类型:Handler bv()
Actual类型:Handler b SessionManager()
在`method'的第二个参数中,即`setter'
在`(< |>)'的第二个参数中,即`method POST setter'


解决方案

您必须绑定 SessionManager 添加到处理程序的上下文中,然后才能使用对其执行操作的函数。这是通过以下方式完成的:

  withTop session $ setInSessionname(convert mname)
- 其中session是如果你还想提交你的会议后(因为你改变了会议和不只是读取一个变量),你需要:

  withSession。 withTop session $ ... 

如果将以下代码添加到主应用程序的snaplet初始化代码,您根本不必担心提交会话,因为它会自动为您完成:

  wrapHandlers withSession 


The App type

data App = App
    { _heist       :: Snaplet (Heist App)
    , _session     :: Snaplet SessionManager
    }

The initializer

...
addRoutes [ ("/ss", companyHandler)
          , ("", heistServe)
          ]
...

The handler

companyHandler :: Handler b v ()
companyHandler = method GET getter <|> method POST setter
  where
    getter = do
        value <- getFromSession "name"
        writeText $ fromMaybe "nothing" value
    setter = do
        mname <- getParam "name"
        setInSession "name" (convert mname)
        getter
    convert = T.pack . B.unpack . (fromMaybe "nothing")

The heistServe has type Handler b (Heist b) ()

Type error:

src/Tutorial.hs:50:52:
    Couldn't match type `v' with `SessionManager'
      `v' is a rigid type variable bound by
          the type signature for companyHandler :: Handler b v ()
          at src/Tutorial.hs:50:1
    Expected type: Handler b v ()
       Actual type: Handler b SessionManager ()
    In the second argument of `method', namely `setter'
    In the second argument of `(<|>)', namely `method POST setter'

解决方案

You have to bind your SessionManager to the context of the handler before you can use functions that operate on it. This is done with:

withTop session $ setInSession "name" (convert mname)
-- Where session is the generated lens for your snaplet

If you also want to commit your session afterwards (because you altered the session and didn't just read a variable), you need to:

withSession . withTop session $ ...

If you add the following piece of code to your main application's snaplet initialization code, you don't have to worry about committing sessions at all, because it is done automatically for you:

wrapHandlers withSession

这篇关于使用snaplet-session输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆