haskell - Function Argument that might or might not instantiate a TypeClass? -


this i'm trying make handler -

getuserstudentsr :: userid -> handler typedcontent getuserstudentsr userid =        getstudententitiesforcoach userid        >>= returntypedcontent . map touserstudentresponse  

where student persistent entity (details not important) ,

getstudententitiesforcoach :: userid -> handlert app io [entity student] getstudententitiesforcoach coachid =      rundb $ selectlist [studentprimarycoachid ==. just(coachid)] []   data userstudentresponse = studentresponse (key student) student  instance tojson userstudentresponse     tojson (studentresponse studentid student) =            object                   [                       "login" .= studentlogin student                       , "studentid" .= studentid                       , "firstname" .= studentfirstname student                       , "lastname" .= studentlastname student                    ]  touserstudentresponse :: (entity student) -> userstudentresponse touserstudentresponse (entity studentid student)      = studentresponse studentid student 

and

returntypedcontent x = selectrep $                            providejson x 

now obviously, doesn't compile unless userstudentresponse instantiates tojson , provides implementation tojson. however, want make returntypedcontent function generic - -

returntypedcontent x = selectrep $                           -- if tojson x --                              providejson x                          -- if tohtml x -- -- note not either or                             providerep $ return $ tohtml $ 

i want this, returntypedcontent can expanded provide returns kinds of contenttypes , based on whether data type used in handler instantiates typeclasses (such tojson), have different things provided for.

is possible without going template haskell?

you use (enable gadts):

data wrapper     json :: tojson => -> wrapper     html :: tohtml => -> wrapper 

and can pattern match:

returntypedcontent (json x) = selectrep (providejson x) returntypedcontent (html x) = selectrep (providerep $ return $ tohtml x) 

you have explicitly wrap data, shouldn't problematic.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -