[Haskell-cafe] type constructor confusion

Stephen Howard stephen at enterity.com
Thu Jun 19 12:28:22 EDT 2008


Cool, "Either" looks like what I'm looking for.  I'll have to look into 
that.  What do I do about the fact that both HttpRequest and 
HttpResponse have some of the same named fields (headers and body, for 
example).  Seems a pain to drop them into separate modules just so I can 
keep the naming conventions consistent.

- Stephen

Ryan Ingram wrote:
> It sounds like you need to split up your types a bit more.
>
> data HttpRequest = HttpRequest ...
>
> data HttpResponse = HttpResponse ...
>
> data HttpMessage = MsgRequest HttpRequest | MsgResponse HttpResponse
> -- alternatively
> -- type HttpMessage = Either HttpRequest HttpResponse
>
> Now you can have functions that take/return just an HttpRequest or
> just an HttpResponse, as well as functions that use either one via
> HttpMessage.  In the latter case, you do need to pattern match to
> decide which one you have.
>
>   -- ryan
>
>
> On 6/18/08, Stephen Howard <stephen at enterity.com> wrote:
>   
>> Thanks Brandon, forgot to send my reply to the list:
>>
>> Ok, so I am confusing things.  Good to know.  So my question is how do I
>> fulfill this scenario?
>>
>> - I have an action that might return either an HttpResponse or an
>> HttpRequest, depending on if the IO in the action determined more work
>> needed doing.  It's here, though I doubt it's "correct" yet:
>>
>> requestHandler :: HttpRequest -> IO HttpResponse
>> requestHandler request = do
>>  session <- sessionHandler request
>>  ret     <- uriHandler     request
>>  case ret of
>>      HttpResponse -> ret
>>      HttpRequest  -> resourceHandler session ret
>>
>> uriHandler :: HttpRequest -> IO HttpMessage
>> sessionHandler :: HttpRequest -> IO HttpSession
>>
>> I've given the uriHandler a signature of IO HttpMessage because the
>> HttpMessage might be either an HttpResponse or an HttpRequest, and I don't
>> know how I should be specifying that.  Ideas?
>>
>> - Stephen
>>
>> Brandon S. Allbery KF8NH wrote:
>>     
>>> On Jun 18, 2008, at 15:31 , Stephen Howard wrote:
>>>
>>>
>>>       
>>>> HttpMessage.hs:36:20: Not in scope: type constructor or class
>>>>         
>> `HttpRequest'
>>     
>>>> The troublesome line is the definition of the cookie function at the end
>>>>         
>> of the code.  I've made
>>     
>>> Right.  "HttpRequest" is a data constructor associated with the type
>>>       
>> constructor "HttpMessage".
>>     
>>> (Data constructors are effectively functions; you used it in the context
>>>       
>> of a type, not a function name.)
>>     
>>>       
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>     


More information about the Haskell-Cafe mailing list