Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction Argument that might or might not instantiate a TypeClass?
    primarykey
    data
    text
    <p>This is what I'm trying to make my Handler look like - </p> <pre><code>getUserStudentsR :: UserId -&gt; Handler TypedContent getUserStudentsR userId = getStudentEntitiesForCoach userId &gt;&gt;= returnTypedContent . map toUserStudentResponse </code></pre> <p>where Student is a Persistent Entity (details mostly not important) and </p> <pre><code>getStudentEntitiesForCoach :: UserId -&gt; HandlerT App IO [Entity Student] getStudentEntitiesForCoach coachId = runDB $ selectList [StudentPrimaryCoachId ==. Just(coachId)] [] data UserStudentResponse = StudentResponse (Key Student) Student instance ToJSON UserStudentResponse where toJSON (StudentResponse studentId student) = object [ "login" .= studentLogin student , "studentId" .= studentId , "firstName" .= studentFirstname student , "lastName" .= studentLastname student ] toUserStudentResponse :: (Entity Student) -&gt; UserStudentResponse toUserStudentResponse (Entity studentId student) = StudentResponse studentId student </code></pre> <p>and</p> <pre><code>returnTypedContent x = selectRep $ do provideJson x </code></pre> <p>Now obviously, this doesn't compile unless UserStudentResponse instantiates ToJSON and provides an implementation for toJSON. However, I want to make the returnTypedContent function generic - something like - </p> <pre><code>returnTypedContent x = selectRep $ do -- if ToJSON x -- provideJSON x -- if ToHTML x -- -- note this is not an either or provideRep $ return $ toHtml $ a </code></pre> <p>I want to do this, so that returnTypedContent can be expanded to provide returns for all kinds of contenttypes and then based on whether the data type used in the handler instantiates certain typeclasses (such as ToJSON), we have different things provided for. </p> <p>Is something like this possible without going into Template Haskell?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload