Question

In function app :: SnapletInit App App I tried my snaplet that has couple of fields:

m <- nestSnaplet "mys" mys $ myexInit  -- Data Myex = Myex {_foo :: Text, ...}

and later in the same function:

let txt1 = myFun  m    -- myFun :: Myex -> Text    

Compiler says:

Couldn't match expected type `SnapletLens a0 b0'
            with actual type `Snaplet Myex'

I tried to define a SnapletLens with

class MyLens b where
  myLens :: SnapletLens b Myex

but then the question is, is this the approach when to get or set the contents of myex-snaplet? And how to refer to the fields of myex? I tried several things, like

let txt = (foo . myLens) m

But they didn't work in the app method. So the problem is, how to access the fields of Snaplet Myex? In the myFun the use of lenses is straightforward, or at least everything compiles.

I just saw another answer that looks very similar. Use subsnaplet during snaplet initialization?

However, if I replace

let txt1 = myFun  m    -- myFun :: Myex -> Text    

with

let txt1 = foo  m    

Compiler says now:

Couldn't match expected type `T.Text -> f0 T.Text'
            with actual type `Snaplet Myex'

I assumed that in the above link the pgPool is a lens to pgs.

br gsp

Was it helpful?

Solution

There are two ways you can get a Myex from a Snaplet Myex. The Snaplet type has a Comonad instance, which means that you can use the extract function. Alternatively, if you don't want to import Control.Comonad, you can use the snapletValue lens.

m :: Snaplet Myex
view snapletValue m :: Myex
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top